Patrick Vogt
Patrick Vogt

Reputation: 916

curl send json with variables

How could I send a JSON-Object via curl to an Incoming Webhook from Microsoft Teams? The JSON is filled with content from a variable.

alarmmeldung=`cat file.txt`
curl -H 'Content-Type: application/json' -d '{"text": $alarmmeldung}' https://incomingwebhookurl

Always I get "Bad payload received by generic incoming webhook" as response.

If I put quotes around the $alarmmeldung then the request could be sent but I get the text $alarmmeldung. What I actually want is the content of $alarmmeldung.

How I have to write the curl statement?

Upvotes: 0

Views: 1663

Answers (2)

kishore kumar
kishore kumar

Reputation: 1

I use the one that answered by @pafjo and it works fine for me but I need to add some message and also use variable in the same message, in this case use the following curl

'$' curl -H 'Content-Type: application/json' -d "{\"text\": 'Repo \"$REPO_NAME\" already exists'}" https://incomingwebhookurl

here repo name is my variable that I pass when ever I trigger github actions using workflow_dispatch

Upvotes: 0

Pafjo
Pafjo

Reputation: 5019

You need replace the single quotes with double quotes to get the variable to be interpolated. You also need to add double quotes around the variable so that it valid json.

curl -H 'Content-Type: application/json' -d "{\"text\": \"$alarmmeldung\"}" https://incomingwebhookurl

If you don't want to do handle the escaping, you can also just included the complete json body as a file:

curl -H 'Content-Type: application/json' -d @file.json https://incomingwebhookurl

Upvotes: 3

Related Questions