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

查看:44
本文介绍了如何通过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手册中写道:"标头可以是[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}."

由于我们没有必发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天全站免登陆