如何通过 Julia HTTP 访问 API [英] How to access an API via Julia HTTP

查看:15
本文介绍了如何通过 Julia HTTP 访问 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Julia 访问 Betfair Exchange API

Access the Betfair Exchange API using Julia

我已经使用 Julia 大约 2 个月了,最近一直在尝试使用 Julia 访问 Betfair API.关于这项服务的注意事项在这里.https://docs.developer.betfair.com/display/1smk3cen4v3lu3yomq5qye0ni/Getting+开始

I've been using Julia for about 2mths now, and have recently been trying to use Julia to access the Betfair API. Note about this service are here. https://docs.developer.betfair.com/display/1smk3cen4v3lu3yomq5qye0ni/Getting+Started

虽然我可以让 Python 示例正常工作(并且我有一个 appKey 和 sessionToken,但未显示),但我无法成功地将这个 Python 翻译成 Julia.

Whilst I can get the Python example working (& I have an appKey & sessionToken though not shown), I've not been able to successfully translate this Python into Julia.

在下面的示例中,我得到了一个 StatusError 400 响应(这是我得到的最接近的响应).其他尝试表明绑定问题可能来自使用 {} 和 ' 的 Python 示例,我尝试将其翻译.

In example below I get a StatusError 400 response (which is the closest I've gotten). Other attempts indicated Bound issues probably from the Python example using {} and ' which Ive attempted to then translate.

我查看了其他 Stackflow 问题,但发现它们没有与此示例相关的复杂性.

I've looked at other Stackflow questions, but found they don't have the complexity associated with this example.

想知道是否有人有任何想法.提前致谢

Wondering if anyone has any thoughts. Thanks in advance

using HTTP

url="https://api.betfair.com/exchange/betting/json-rpc/v1"
header = ""X-Application" : "appKey", "X-Authentication" : "sessionToken" ,"content-type" : "application/json" "

jsonrpc_req=""jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": {"filter":{ }}, "id": 1"

response = HTTP.post(url, data=[jsonrpc_req], headers=[header])

println(response.text)

预期结果.在 Python 中,我得到了必发体育和市场的摘要.

Expected Results. In Python, I get a summary of Betfair Sports and Market's.

{"jsonrpc":"2.0","result":[{"eventType":{"id":"1","name":"Soccer"},"marketCount":10668},{"eventType":{"id":"2","name":"Tennis"},"marketCount":4590},{"eventType":{"id":"3","name":"Golf"},"marketCount":43},{"eventType":{"id":"4","name":"Cricket"},"marketCount":394},{"eventType":{"id":"5","name":"Rugby Union"},"marketCount":37},{"eventType":{"id":"1477","name":"Rugby League"},"marketCount":24},{"eventType":{"id":"6","name":"Boxing"},"marketCount":27},{"eventType"
...etc...

目前得到

HTTP.ExceptionRequest.StatusError(400, HTTP.Messages.Response:
400 Bad Request.

推荐答案

虽然与特定 REST 服务的交互是一个特定于问题的问题,但这里是一般准则.

While the interaction with a particular REST service is a problem-specific issue here are the general guidelines.

首先,您需要正确格式化 headers - HTTP.jl 手册中写道:headers 可以是任何集合,其中 [string(k) => string(v)for (k,v) in headers] 产生 Vector{Pair}."

Firstly, you need to properly format headers - HTTP.jl manual reads: "headers can be any collection where [string(k) => string(v) for (k,v) in headers] yields Vector{Pair}."

由于我们没有 Betfair API 密钥,让我们看一个使用 https://postman-echo.com/ 的更通用的示例,这是一个免费的简单 API 测试,仅以 JSON 形式返回无论它得到什么作为输入.

Since we do not have Betfair API key let's have a look on a more generic example using https://postman-echo.com/ which is a free simple API testing that simply returns as JSON whatever it gets as the input.

using HTTP
using JSON

headers = (("X-Application","appKey"),("X-Authentication","sessionToken"),
           ("content-type","application/json"))

url="https://postman-echo.com/post"

req = Dict("jsonrpc" => "2.0", "params" => Dict("filet" => Dict()))

response = HTTP.post(url, headers, JSON.json(req))
response_text = String(response.body)
json_obj = JSON.parse()

现在让我们解析 postman-echo.com 的输出:

Now let us parse the output from postman-echo.com:

julia> display(JSON.parse(response_text))
Dict{String,Any} with 7 entries:
  "headers" => Dict{String,Any}("x-forwarded-port"=>"443","host"=>"postman-echo.com","x-application"=>"appKey","content-type"…  "json"    => Dict{String,Any}("params"=>Dict{String,Any}("filet"=>Dict{String,Any}()),"jsonrpc"=>"2.0")
  "files"   => Dict{String,Any}()
  "args"    => Dict{String,Any}()
  "data"    => Dict{String,Any}("params"=>Dict{String,Any}("filet"=>Dict{String,Any}()),"jsonrpc"=>"2.0")
  "url"     => "https://postman-echo.com/post"
  "form"    => Dict{String,Any}()

您可以轻松地将上述代码应用于任何 RESTful JSON API.

You can easily adopt the above code to any RESTful JSON API.

这篇关于如何通过 Julia HTTP 访问 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆