How do I add a variable to this curl command?

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 :) .)


Just to put one more solution here:

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

Basically, " is a quote to handle the following string together, \" escapes the quote, and ${varname} is a variable.


I tend to use heredocs when building JSON for use with curl:

curl -s -X POST $URL -d@- <<EOF
[
    {
        "id": 101,
        "text": "$variable"
    }
]
EOF

Tags:

Linux

Bash

Curl