3

I'm trying to send a POST request with curl, but I'd like to store the json data in a variable in order to resend it if an error occur. I used this code:

jsonvariable="{"ora" : "value1", "temp" : "value2", "rh" : "value3", "lat" : "value4", "longi" : "value5"}"


curl -X POST -H "Content-Type: application/json" -d '$jsonvariable' http://localhost:8080/updates

but the format after the -d option is not correct. Can you help me?

this code:

curl -X POST -H "Content-Type: application/json" -d '{"ora" : "value1", "id" : "value2", "temp" : "value3","rh" : "value4", "lat" : "value5", "longi" : "value6"}' http://localhost:8080/updates

gives no errors instead

3

You used single quotes which prevent the variable from being expanded. Use double quotes instead:

curl -H "Content-Type: application/json" -d "$jsonvariable" http://localhost:8080/updates

If your variable content contains double quotes you can quote them with e.g. backslashes.

You can omit -X POST here because POST is the default method if you specify data to send with -d.

Further reading on quoting in bash: How do I enter a file or directory with special characters in its name?

| improve this answer | |
  • That's reducing the problem to a more difficult one. How do I properly escape $jsonvariable then? – Dirk Groeneveld Mar 20 at 22:57
  • @DirkGroeneveld How is proper quotation a more difficult problem? It seems like you have a new question, if so please ask it: Ask Question. – dessert Mar 21 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.