How can I post a json string with curl that has characters that need escaping?
<p>I've got a shell script I've been using to post stuff to a hipchat channel. It works ok until I try and send a message that has characters that need escaping. I run the command like so (note the extra backslash in there to cause a problem)</p>

<pre><code>/usr/local/bin/hipchatmsg.sh "my great message here \ " red
</code></pre>

<p>And my code in my bash script (hipchatmsg.sh) that matters is this:</p>

<pre><code># 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 &amp;

// $server and $room are defined earlier

exit 0
</code></pre>

<p>If I try and run the command above with any characters that need escaping, I will get an error like this:  </p>

<pre><code>{
    "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"
    }
}
</code></pre>

<p>I found something kind of similar on here where the best advice was to try sending the curl post with --data-urlencode, so I tried like this:  </p>

<pre><code>/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 &amp;
</code></pre>

<p>But this had no effect.</p>

<p>What am I missing here?</p>