4

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

dessert
  • 40,956
yuki182
  • 43

1 Answers1

6

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?

dessert
  • 40,956