I have a JSON list and I am trying to POST a particular key: value from the list to the slack webhook using curl. I am using jq to get the value for a specific key
This works fine:
echo {{job.message}} | jq -r '.[0] | .status'
This does not work:
curl -vvv -X POST -H "Content-type: application/json" --data \"{\"text\":\"{{job.message}} | jq -r ''.[0] | .status''\"}\" https://hooks.slack.com/services/xxxxx
job.message
is a variable with json list
When I output the result using echo in shell, it works fine but when I use curl it show invalid payload or .key not found even though the key is present.
CodePudding user response:
In your text
value, there is no mention of the command echo ...
, you may use a subshell to evaluate the result. Also, you can use single quote for --data
instead of double quote to avoid escaping:
curl -vvv -X POST \
-H "Content-type: application/json" \
--data '{"text":"'"$(echo $job_message | jq -r '.[0].status')"'"}' \
"https://hooks.slack.com/services/xxxxx"
Also you can use jq -n
to build the JSON object:
curl -vvv -H "Content-type: application/json" \
-d "$(jq -n --arg text $job_message '{"text": ($text | fromjson)[0].status}')" \
"https://hooks.slack.com/services/xxxxx"
You can also pipe jq
command output to the body like this and use jq
only to build the json object:
jq -n --arg text $job_message '{"text": ($text | fromjson)[0].status}' | \
curl -vvv -H "Content-Type: application/json" -d @- "https://hooks.slack.com/services/xxxxx"