How to send a simple "GET" or "POST" with parameters

The data provider has an example in python:

url = "xxx"
parameter = {
    "dataset": "TaiwanStockInfo",
    "token": "", # 參考登入,獲取金鑰
}
resp = requests.get(url, params=parameter)

How to translate these simple python code into Julia language?
I try many times but fail…below is my fail example:

url="xxx"
parameter = Dict("dataset" => "TaiwanVariousIndicators5Seconds",
        "start_date" => "2020-07-01",
        "end_date" => "2020-07-27",
        "token" => "xxx")
pms=JSON.json(parameter)
r=HTTP.request("GET", url,["Content-Type" => "application/json"],pms)

or

url="xxx"
parameter = Dict("dataset" => "TaiwanVariousIndicators5Seconds",
        "start_date" => "2020-07-01",
        "end_date" => "2020-07-27",
        "token" => "xxx")
r=HTTP.request("GET", url,["Content-Type" => "application/json"],parameter)
parameter = ["dataset"=> "TaiwanStockInfo",
        "token" => "xxx"]
HTTP.request("GET", url,
             ["Content-Type" => "application/json"],
             parameter)

All above code are fail and return error message like this:
“HTTP.ExceptionRequest.StatusError(422, “GET”, “/api/v4/data”, HTTP.Messages.Response:”

Probably the easiest way would be:

using URIs

uri = URI("https://www.google.com/search")
uri = URI(uri; query=Dict("q" => "julia"))

HTTP.request("GET", string(uri))

The first URI() defines the bulk of the URL. The second URI() adds the query string (parameters) to the URL. Then you convert it to a string, URI will URL encode the query string for you, and pass it to HTTP.request().

3 Likes

Your suggestion is work! Thanks.