1

I have a below curl command which works fine and I get the response back. I am posting json data to an endpoint which gives me response back after hitting it.

curl -v 'url' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: url' --data-binary '{"query":"\n{\n  data(clientId: 1234, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"}' --compressed

Now I am trying to read the binary data from temp.txt file outside but somehow it doesn't work and I get an error -

curl -v 'url' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: url' --data-binary "@/Users/david/Downloads/temp.txt" --compressed

Below is the content I have in my temp.txt file -

Original "temp.txt" file

{
  data(clientId: 1234, filters: [{key: "o", value: 100}], key: "world") {
    title
    type
    pottery {
      text
      pid
      href
      count
      resource
    }
  }
}

This is the error I am getting -

.......
* upload completely sent off: 211 out of 211 bytes
< HTTP/1.1 500 Internal Server Error
< date: Fri, 28 May 2021 23:38:12 GMT
< server: envoy
< content-length: 0
< x-envoy-upstream-service-time: 1
<
* Connection #0 to host url left intact
* Closing connection 0

Is there anything wrong I am doing?

Also if I copy the exact same content in the temp.txt file that I have in my original curl command with \n in it then it works fine.

Updated "temp.txt" file

Meaning if I keep the content like this in temp.txt file then it works fine from my second curl -

{"query":"\n{\n  data(clientId: 1234, filters: [{key: \"o\", value: 100}], key: \"world\") {\n    title\n    type\n    pottery {\n      text\n      pid\n      href\n      count\n      resource\n    }\n  }\n}"}

It means I need to find a way to convert new lines to \n manually from temp.txt file before sending the curl request or is there any other way?

6
  • You say 'Content-Type: application/json, but your original temp.txt is definitely not JSON.
    – muru
    May 29, 2021 at 4:19
  • Does it matter in this case? It works fine for my first curl call without any issues but my second curl command only works fine with my Updated "temp.txt" file. I am confuse what I should do so that it can work with my Original "temp.txt" file.
    – David Todd
    May 29, 2021 at 4:21
  • Well, if you tell your server you're going to send JSON and then send some garbage at it, what did you expect it to do?
    – muru
    May 29, 2021 at 4:23
  • yeah that I understand but then why it works fine on my first curl request? It shouldn't work for the first case as well right? With that in mind I am just trying to extract body content from outside file instead of hardcoding it in the curl command line.
    – David Todd
    May 29, 2021 at 4:24
  • Why? {"query":"\n{\n data(clientId: 1234, filters: [{key: \"o\", value: 100}], key: \"world\") {\n title\n type\n pottery {\n text\n pid\n href\n count\n resource\n }\n }\n}"} seems like valid enough JSON.
    – muru
    May 29, 2021 at 4:25

1 Answer 1

1

Your data payload is a JSON document containing a query key. The value of that key is a JSON-encoded document, possibly describing some form of query, which is not in itself a JSON document. Newlines are encoded as \n in JSON values, and the JSON parser that the server is using would translate these into literal newlines when it receives your request.

Your attempt to put the decoded query value in a separate file and pass that in your curl call fails, because the API you are talking to expects the data to be a JSON document with a JSON-encoded value for the query key.

The correct thing to do to offload the query into a separate file is to do exactly what you did in your last example. Put the JSON document with the encoded query in a file and reference it using --data-binary @filename on the curl command line.

curl \
    --header 'Content-Type: application/json' \
    --data-binary '@/Users/david/Downloads/temp.txt' "$API_ENDPOINT"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .