如何通过HTTP访问API

如何通过HTTP访问API,http,julia,Http,Julia,使用Julia访问Betfair Exchange API 现在我已经使用Julia大约2/4了,最近我尝试使用Julia访问Betfair API。 关于这项服务的注意事项在这里。 虽然我可以让Python示例正常工作(我有一个appKey和sessionToken,虽然没有显示),但我还不能成功地将这个Python翻译成Julia 在下面的示例中,我得到一个StatusError 400响应(这是我得到的最接近的响应)。其他尝试指出了绑定问题,可能来自Python示例中使用{}和“的绑定问

使用Julia访问Betfair Exchange API

现在我已经使用Julia大约2/4了,最近我尝试使用Julia访问Betfair API。 关于这项服务的注意事项在这里。

虽然我可以让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中,我得到了Betfair Sports和Market的摘要

{"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服务的交互是一个特定于问题的问题,但这里有一些一般的指导原则

首先,您需要正确设置
标题的格式
-HTTP.jl手册如下:“标题可以是任何集合,其中
[标题中(k,v)的字符串(k)=>字符串(v)]
产生
向量{Pair}

由于我们没有Betfair 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()
现在让我们分析一下postmanecho.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。

感谢Przemyslaw Szufel的回复。在经历了几天的挫折之后,我设法在这里使用Excel/VBA示例使API的第一部分正常工作:(我对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.1200ok 日期:2019年2月17日星期日17:28:08 GMT 服务器:Cougar-4.4.2(Cougar 2模式) 缓存控制:没有缓存 内容类型:application/json 更改:接受编码,用户代理 内容长度:1850

{“jsonrpc”:“2.0”,“结果”:[{“事件类型”:{“id”:“1”,“名称”:“足球”},“市场计数”:6553},{“事件类型”:{“id”:“2”,“名称”:“网球”},“市场计数”:5511},{“事件类型”:{“id”:“3”,“名称”:“高尔夫”},“市场计数”:34} 等等

希望这对其他人也有帮助