Peder Lewenhaupt
Peder Lewenhaupt

Reputation: 33

API POST request in Julia

I am trying to convert some Python code to Julia. Here is the Python code:

url = "http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101G/BefUtvKon1749"

json = {
  "query": [
    {
      "code": "Kon",
      "selection": {
        "filter": "item",
        "values": [
          "1",
          "2"
        ]
      }
    },
    {
      "code": "ContentsCode",
      "selection": {
        "filter": "item",
        "values": [
          "000000LV"
        ]
      }
    }
  ],
  "response": {
    "format": "px"
  }
}

r = requests.post(url=url, json=json)

Below is the Julia code, that is not working, with this error message:

syntax: { } vector syntax is discontinued around path:8
top-level scope at population_data.jl:8

using DataFrames, DataFramesMeta, HTTP, JSON3

url = "http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101G/BefUtvKon1749"

json = {
  "query": [
    {
      "code": "Kon",
      "selection": {
        "filter": "item",
        "values": [
          "1",
          "2",
          "1+2"
        ]
      }
    },
    {
      "code": "ContentsCode",
      "selection": {
        "filter": "item",
        "values": [
          "000000LV"
        ]
      }
    }
  ],
  "response": {
    "format": "px"
  }
}

r = HTTP.post(url, json)

My attempts to solve this are the following:

  1. Convert the json variable to a string using """ around it.
  2. Converting the JSON string to Julia data types, using JSON3.read()
  3. Passing the converted JSON string to the POST request. This gives the following error:

IOError(Base.IOError("read: connection reset by peer (ECONNRESET)", -54) during request(http‍://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101G/BefUtvKon1749)

None of it works, and I am not even sure that it is about the JSON format. It could be that I am passing the wrong parameters to the POST request. What should I do?

Upvotes: 2

Views: 1670

Answers (2)

jmarina
jmarina

Reputation: 375

here is a generic JSON POST request in Julia without building a dictionary etc, works with any json; the raw multiline JSON is enclosed in triple quotes """; the response is set into a string so it can be processed or displayed later:

using JSON, HTTP # note JSON not required in this example
jsonreq = """{
   "a": "b",
   "c": "d",
   "e": [[1,2],[3.3,4.4]],
   "f": ["g","h"],
   "i": "j"
}"""
ans = HTTP.request("POST", "https://your/rest/api",
      ["Content-Type" => "application/json", 
       "Authorization" => ""], jsonreq)
str = String(ans.body);
print(str)

Upvotes: 0

One way of solving this consists in building the parameters as native julia data structures, and use JSON to convert and use them as the body of your PUT request:

Dictionaries in julia are built using a syntax like Dict(key => value). Arrays are built using a standard syntax: [a, b, c]. The julia native data structure equivalent to your parameters would look like this:

params = Dict(
"query" => [
    Dict("code" => "Kon",
         "selection" => Dict(
             "filter" => "item",
             "values" => [
                 "1",
                 "2",
                 "1+2"
             ]),
         ),
    Dict("code"=> "ContentsCode",
         "selection" => Dict(
             "filter" => "item",
             "values" => [
                 "000000LV"
             ]),
         ),
],
"response" => Dict(
    "format" => "px"
))

Then, you can use JSON.json() to build the JSON representation of it as a string and pass it to the HTTP request:

using HTTP
using JSON

url = "http://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101G/BefUtvKon1749"

# send the request
r = HTTP.request("POST", url,
                 ["Content-Type" => "application/json"],
                 JSON.json(params))

# retrieve the response body as a string
b = String(r.body)

Upvotes: 3

Related Questions