如何通过Julia HTTP

时间:2019-02-10 03:13:08

标签: http julia

使用Julia访问Betfair Exchange API

我现在已经使用Julia约2个月了,最近一直在尝试使用Julia来访问必发API。 有关此服务的说明在这里。 https://docs.developer.betfair.com/display/1smk3cen4v3lu3yomq5qye0ni/Getting+Started

虽然我可以使Python示例正常工作(虽然未显示,但我有一个appKey和sessionToken),但我仍无法成功地将此Python转换为Julia。

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

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

想知道是否有人有想法。 预先感谢

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中,我获得了必发体育与市场的摘要。

{"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.

2 个答案:

答案 0 :(得分:2)

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

首先,您需要正确设置headers的格式-HTTP.jl手册中写道:“ 标头可以是[string(k) => string(v) for (k,v) in headers]产生Vector{Pair}的任何集合。” < / p>

由于我们没有必发API密钥,因此让我们来看一个使用https://postman-echo.com/的更通用的示例,该示例是免费的简单API测试,无论输入得到什么,它都将作为JSON返回。

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的输出:

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。

答案 1 :(得分:0)

感谢Przemyslaw Szufel的回应。经过几天的挫败后,我设法在此处使用Excel / VBA示例来使API的第一部分正常工作:https://github.com/betfair/API-NG-Excel-Toolkit(我对Python示例的翻译无效)。 您的评论对理解如何构造多个标头以及使用Dict(=>)而不是字符串进行上述操作很有帮助。

using HTTP
using JSON

const ListEventTypesMethod = "listEventTypes"
const AppKey = "appKey"
const Session = "sessionToken"

function SendRequest(Url, AppKey, Session, Data)
    headers = (("X-Application", AppKey),
                ("content-type", "application/json"),
                ("Accept", "application/json"),
                ("X-Authentication", Session))

  HTTP.get(Url,headers,JSON.json(Data))

end

function ParseJsonRpcResponseToCollection(Response)
    ParseJsonRpcResponseToCollection = JSON.parse(Response)
end

function GetJsonRpcUrl()
    GetJsonRpcUrl = "https://api.betfair.com/exchange/betting/json-rpc/v1/"
end

function MakeJsonRpcRequestString(Method, RequestString)
#MakeJsonRpcRequestString = "{""jsonrpc"": ""2.0"", ""method"": ""SportsAPING/v1.0/" & Method & """, ""params"": " & RequestString & ", ""id"": 1}"
MakeJsonRpcRequestString = Dict("jsonrpc" => "2.0", "method" =>"SportsAPING/v1.0/"*Method, "params" => RequestString, "id"=> 1 )
end

function GetListEventTypesRequestString()
    #GetListEventTypesRequestString = "{""filter"":{}}"
     GetListEventTypesRequestString=Dict("filter" =>Dict())
end


Request = MakeJsonRpcRequestString(ListEventTypesMethod, GetListEventTypesRequestString())
ListEventTypesResponse = SendRequest(GetJsonRpcUrl(), AppKey, Session, Request)

回复

HTTP / 1.1 200确定 日期:2019年2月17日星期日17:28:08 GMT 服务器:Cougar-4.4.2(Cougar 2模式) 缓存控制:无缓存 内容类型:application / json 不同:接受编码,用户代理 内容长度:1850

{“ jsonrpc”:“ 2.0”,“ result”:[{“ eventType”:{“ id”:“ 1”,“ name”:“ Soccer”},“ marketCount”:6553},{“ eventType “:{” id“:” 2“,”名称“:”网球“},” marketCount“:5511},{” eventType“:{” id“:” 3“,”名称“:”高尔夫“}, “ marketCount”:34} 等等...

希望这对其他人也有帮助。