Using HTTP to do a post request where some parameters are JSON strings

I’m having a problem where, when trying to write data into a server’s api using post, I cannot seem to send the correct format of json strings, i.e. without the \" characters in between that we get when we do json(string). I understand this formatting is here because of the ambiguity and how julia would try to parse a normal json string into many strings instead. This is a database table where some columns expect a long text string, which does not need to be a json for the writing to be successful.

This is not supposed to be ran but here’s an example:

using DataStructures
using Dates
using JSON
using HTTP

myindices = 1:10
col1 = SortedDict(zip(myindices, rand(length(myindices))))
col2 = SortedDict(zip(myindices, rand(length(myindices))))
writedate = Date(now())
body = Dict("writedate" => writedate, "col1" => json(col1), "col2" => json(col2))

url = "http://somethingsomething.com/useThisToWrite"
header = Dict("password" => "mypassword")

response = HTTP.post("POST", url, header, body)
println(response.body)

And then what happens is that on the server what gets inserted ends up looking like

"{\"1\":0.4963953108428159,\"2\":0.5333778360359551,\"3\":0.5353534348233446,\"4\":0.5371311426294897, ...

But I think what we need to be there is instead the usual

{"1":0.4963953108428159,"2":0.5333778360359551,"3":0.5353534348233446,"4":0.5371311426294897, ...

Otherwise the server cannot parse it without us modifying it completely.

If I was writing to a file, I would just use the write function and not face this problem.

It looks like you are encoding twice. Try

body = Dict("writedate" => writedate, "col1" => col1, "col2" => col2)
response = HTTP.post(url, header, json(body))

Thanks, you are right. It turns out there was also a bug on the api’s JSON parsing as well.