Bash – How to add a variable to this curl command

bashcurllinux

I have a curl command that sends a string of text to the server and I've been trying to figure out how to either have the string of text come from a file or from a bash variable. The command looks like this:

curl -X POST -u "apikey:<apikey>"
--header "Content-Type: application/json"
--data '{"text": "<variable>"}'
"<url>"

I can't figure out how to get a variable in there. I've tried replacing with $variable and $(< file) but I don't know how to get those to spit out text without an echo and I can't echo in a curl.

Best Answer

Stop the single quoted string, follow with the variable expansion, posibly double quoted, and resume the single quoted string:

--data '{"text": "'"$variable"'"}'

($variable should still expand to something that together with the surroundings forms legal JSON, or else the other side probably won't be very happy :) .)

Related Question