我有一个 shell 脚本,我一直用它来将内容发布到 hipchat 频道。它工作正常,直到我尝试发送包含需要转义的字符的消息。我像这样运行命令(注意其中额外的反斜杠会导致问题)
/usr/local/bin/hipchatmsg.sh "my great message here \ " red
我的 bash 脚本 (hipchatmsg.sh) 中重要的代码是这样的:
# Make sure message is passed
if [ -z ${1+x} ]; then
echo "Provide a message to create the new notification"
exit 1
else
MESSAGE=$1
fi
// send locally via curl
/usr/bin/curl -H "Content-Type: application/json" \
-X POST \
-k \
-d "{\"color\": \"$COLOR\", \"message_format\": \"text\", \"message\": \"$MESSAGE\" }" \
$SERVER/v2/room/$ROOM_ID/notification?auth_token=$AUTH_TOKEN &
// $server and $room are defined earlier
exit 0
如果我尝试使用任何需要转义的字符运行上面的命令,我将收到如下错误:
{
"error": {
"code": 400,
"message": "The request body cannot be parsed as valid JSON: Invalid \\X escape sequence u'\\\\': line 1 column 125 (char 124)",
"type": "Bad Request"
}
}
我在这里发现了类似的东西,最好的建议是尝试使用 --data-urlencode 发送卷曲帖子,所以我尝试这样:
/usr/bin/curl -H "Content-Type: application/json" \
-X POST \
-k \
-d --data-urlencode "{\"color\": \"$COLOR\", \"message_format\": \"text\", \"message\": \"$MESSAGE\" }" \
$SERVER/v2/room/$ROOM_ID/notification?auth_token=$AUTH_TOKEN &
但这没有效果。
我在这里缺少什么?