Getting invalid payload when trying to cURL slack

梦想的初衷 提交于 2021-01-29 06:21:43

问题


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" )

回答1:


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

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



回答2:


You are trying to post www-form-urlencoded data to an endpoint which is expecting JSON.

Apart from that your code works fine.




回答3:


This works fine for me:

curl -X POST -H \"Content-type: application/json\" --data \"{\\\"text\\\":\\\"Here your text!!\\\"}\" <host_slack>


来源:https://stackoverflow.com/questions/51330529/getting-invalid-payload-when-trying-to-curl-slack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!