1

I'm trying to send a message to slack using the curl command. I wanted to use a variable in the curl command

curl -X POST -H 'Content-type: application/json' --data '{"text": $text }' "https://hooks.slack.com/services/blabla"

here the $text refers to the text I want to send.

this is how $text looks like

text="The following jobs are in queue: $jobs"

If you try echo'ing thext, it will be something like

echo $text
The following jobs are in queue: "job1" "job2"

But the above curl command is giving invalidpaylod

Tula
  • 307
  • 6
  • 15
  • 3
    Use `'{ "text": "'"$text"'" }'` instead. If the text may contain characters that has to be encoded, use [tag:jq] to generate the data (e.g.: `data=$(jq -n --arg text "$text" '{$text}')`) and use it in the curl invocation like `curl ... --data "$data" ...` – oguz ismail Dec 21 '20 at 10:32

1 Answers1

3

You have the text variable in single quotes and so the content won't be expanded. Put single quotes around the variable:

curl -X POST -H 'Content-type: application/json' --data '{"text": '$text' }' "https://hooks.slack.com/services/blabla"
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Thanks, but this is resulting in the following error Could not resolve host: following curl: (6) Could not resolve host: jobs curl: (6) Could not resolve host: are curl: (6) Could not resolve host: in curl: (6) Could not resolve host: queue – Tula Dec 21 '20 at 09:50