How to send JSON as variable with bash curl?
21,661
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?
Author by
yuki182
Updated on September 18, 2022Comments
-
yuki182 over 1 year
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
-
Dirk Groeneveld about 4 yearsThat's reducing the problem to a more difficult one. How do I properly escape
$jsonvariable
then? -
dessert about 4 years@DirkGroeneveld How is proper quotation a more difficult problem? It seems like you have a new question, if so please ask it: Ask Question.