Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

How to access an API via Julia HTTP

Access the Betfair Exchange API using Julia

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

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.

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.

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)

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

Currently get

HTTP.ExceptionRequest.StatusError(400, HTTP.Messages.Response:
400 Bad Request.
question from:https://stackoverflow.com/questions/65875303/how-to-use-rest-api-in-julia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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()

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}()

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...