title: Trouble with bash shell script, attempting to POST variable JSON data using cURL
body: <p>I'm having trouble with a bash shell script, attempting to POST variable JSON data using cURL. I'm running from a Mac. I can successfully post static data but I can't seem to figure out how to incorporate variables.</p>
  
  <p>I introduced &lt;room&gt; and &lt;token&gt; for the sake of these examples.</p>
  
  <p>This script works successfully:</p>
  
  <pre><code>#!/bin/bash
  curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed", "message_format":"text" }' https://api.hipchat.com/v2/room/&lt;room&gt;/notification?auth_token=&lt;token&gt;
  </code></pre>
  
  <p>Now, I would like to introduce a formatted date. This script posts successfully, but the "$now" is posted literally: i.e. "Build failed $now" rather than "Build failed 10-28-2014"</p>
  
  <pre><code>#!/bin/bash
  now=$(date +"%m-%d-%Y")
  curl -X POST -H "Content-Type: application/json" --data '{ "color":"red", "message":"Build failed $now", "message_format":"text" }' https://api.hipchat.com/v2/room/&lt;room&gt;/notification?auth_token=&lt;token&gt;
  </code></pre>
  
  <p>I attempted to format the JSON payload with printf like so. The date string is replaced properly. However, this fails with an error: "The request body cannot be parsed as valid JSON: No JSON object could be decoded: line 1 column 0 (char 0)" - so it seems like I'm misusing $payload.</p>
  
  <pre><code>#!/bin/bash
  now=$(date +"%m-%d-%Y")
  payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
  curl -X POST -H "Content-Type: application/json" --data $payload https://api.hipchat.com/v2/room/&lt;room&gt;/notification?auth_token=&lt;token&gt;
  </code></pre>
  
  <p>Finally, I attempted to eval the entire command. This fails by hanging and it could be that I'm misusing escapes. I've tried many variations of escaping.</p>
  
  <pre><code>#!/bin/bash
  now=$(date +"%m-%d-%Y")
  payload=$(printf "\'{\"color\":\"red\",\"message\":\"Build failed %s\",\"message_format\":\"text\"}\'" $now)
  cmd=$(curl -X POST -H \"Content-Type: application\/json\" --data '{\"color\":\"red\",\"message\":\"Build failed $now\",\"message_format\":\"text\"}' https:\/\/api.hipchat.com\/v2\/room\/&lt;room&gt;\/notification?auth_token=&lt;token&gt;)
  eval $cmd
  </code></pre>
  
  <p>I found this <a href="https://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest">question</a> to be somewhat helpful and I've also read this <a href="http://curl.haxx.se/docs/httpscripting.html#POST" rel="noreferrer">cURL tutorial</a>. These deal with static data and I think I'm just missing some fundamental bash scripting. Thank you in advance for your help.</p>