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

**URL:** <https://discourse.julialang.org/t/using-http-to-do-a-post-request-where-some-parameters-are-json-strings/106277>\
**Category:** General Usage\
**Tags:** json, http\
**Created:** [November 15, 2023, 6:26pm UTC](https://discourse.julialang.org/t/using-http-to-do-a-post-request-where-some-parameters-are-json-strings/106277 "2023-11-15T18:26:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [November 15, 2023, 6:26pm UTC](https://discourse.julialang.org/t/using-http-to-do-a-post-request-where-some-parameters-are-json-strings/106277/1 "2023-11-15T18:26:23Z")

</div>

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:

```julia
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

```julia
"{\"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

```julia
{"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.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [November 15, 2023, 10:30pm UTC](https://discourse.julialang.org/t/using-http-to-do-a-post-request-where-some-parameters-are-json-strings/106277/2 "2023-11-15T22:30:15Z")

</div>

It looks like you are encoding twice. Try

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

```

---

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [November 16, 2023, 11:22am UTC](https://discourse.julialang.org/t/using-http-to-do-a-post-request-where-some-parameters-are-json-strings/106277/3 "2023-11-16T11:22:03Z")

</div>

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