Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting invalid payload when trying to cURL slack

I am trying to cURL slack when nginx fails to reload. I am able to check that nginx failed and can cURL slack when it does. I am running into an invalid payload error when trying to include the error message from the failed reload within my cURL command. This is the script I have to do that:

#!/bin/bash

OUTPUT=$(nginx -s reload 2>&1 > /dev/null >/mnt/efs/out.txt)

ESCAPE=$(echo $OUTPUT | sed 's/"/\"/g' | sed "s/'/\'/g" )

nginx -t || curl -X POST --data-urlencode 'payload={"username": "reload-monitor-lizard", "text": "'"${ESCAPE}"'", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/xxxxxxx/xxxxx/xxxxxxxxxxxxxxxxxxxx

I've tried playing around with the quotes, but it either still gives me the invalid payload error or sends

${ESCAPE}

as the message. Any suggestions on what I am doing wrong?

Update:

Fixed this by replacing the double quotes in my variable with single quotes like this:

ESCAPE=$(echo $OUTPUT | sed "s/\"/'/g" )
like image 735
user2303670 Avatar asked Nov 01 '25 03:11

user2303670


2 Answers

Fixed by replacing double quotes in the variable with single quotes. Like so:

ESCAPE=$(echo $OUTPUT | sed "s/\"/'/g" )
like image 134
user2303670 Avatar answered Nov 02 '25 17:11

user2303670


This works fine for me:

curl -X POST -H \"Content-type: application/json\" --data \"{\\\"text\\\":\\\"Here your text!!\\\"}\" <host_slack>
like image 23
Ernesto Andrade Avatar answered Nov 02 '25 17:11

Ernesto Andrade



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!