Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

payload is not valid JSON

I'm using curl to send JSON to an API endpoint. However, somewhere in the bash chain it is getting messed up.

Is there something special to know about encoding with curl?

If I construct the payload like this:

 PAYLOAD='payload={"channel": "github", "username": "webhookbot", "icon_emoji": ":ghost:", "text": "'
 PAYLOAD+=$1
 PAYLOAD+=' " }'

 echo $PAYLOAD
 curl -X POST --data-urlencode "$PAYLOAD" $SLACKPOSTURL
 echo "sent"

I'll get back an error

Payload was not valid JSONsent

however if i just hardwire to assign a variable with the output

PAYLOAD='payload={"channel": "github", "username": "webhookbot", "icon_emoji": ":ghost:", "text": "LAST_COMMIT Merge pull request #558 from dcsan/boteditor Boteditor " }'

then it will go through fine.

Is there something that a simple assignment is doing differently vs. concatenating strings? In the console the output looks identical.

FWIW some messages go through but content like this:

LAST_COMMIT Merge pull request #558 from dcsan/boteditor Boteditor

will only go through if hardcoded in. so its not the other end afaican see, its something to do with the way messages are built.

like image 694
dcsan Avatar asked May 06 '26 19:05

dcsan


1 Answers

I guess you want to concatenate values into your variable. But += is not the way to do so.

To concatenate strings in a variable you need to say:

PAYLOAD="$PAYLOAD $1"

All together it would be something like the following. Note the need to use " so that the variable $PAYLOAD is expanded and the usage of \" to store a literal double quote:

PAYLOAD='payload={"channel": "github", "username": "webhookbot", "icon_emoji": ":ghost:", "text": "'
PAYLOAD="$PAYLOAD $1 \" }"

echo "$PAYLOAD"
curl -X POST --data-urlencode "$PAYLOAD" $SLACKPOSTURL
echo "sent"
like image 73
fedorqui 'SO stop harming' Avatar answered May 09 '26 10:05

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!