4

Some research revealed a few useful stackexchange posts, namely expanding variable in CURL, but that given answer doesn't seem to properly handle bash variables that have spaces in them.

I am setting a variable to the output of awk, parsing a string for a substring (actually truncating to 150 characters). The string I am attempting to POST via curl has spaces in it.

When I use the following curl arguments, the POST variable Body is set to the part of the string before the first space.

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' -d 'From=DIDfrom' -d 'To=DIDto' -d 'Body="'$smsbody'" -u SECGUID

smsbody is set as:

smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)}')"

So the only portion of smsbody that is POSTed is $HOSTNAME$ (which happens to be a string without any space characters).

What is the curl syntax I should use to nest the bash variable properly to expand, but be taken as a single data field?

Seems pretty trivial, but I messed with quotes for a while without luck. I figure someone with better CLI-fu can handle it in a second.

Thanks!

Community
  • 1
  • 1
brandeded
  • 2,080
  • 6
  • 25
  • 52

1 Answers1

3

It looks like you have an extra single quote before Body. You also need double quotes or the $smsbody won't be evaluated.

Try this:

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' \
    -d 'From=DIDfrom' -d 'To=DIDto' -d "Body=$smsbody" -u SECGUID

If the $s are still an issue (I don't think spaces are), try this to prepend a \ to them:

smsbody2=`echo $smsbody | sed 's/\\$/\\\\$/g'`
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' \
    -d 'From=DIDfrom' -d 'To=DIDto' -d "Body=$smsbody2" -u SECGUID

If I run nc -l 5000 and change the twilio address to localhost:5000, I see the smsbody variable coming in properly.

matt@goliath:~$ nc -l 5000POST / HTTP/1.1
Authorization: Basic U0VDR1VJRDphc2Q=
User-Agent: curl/7.21.6 (x86_64-apple-darwin10.7.0) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.5 libidn/1.20
Host: localhost:5000
Accept: */*
Content-Length: 45
Content-Type: application/x-www-form-urlencoded

From=DIDfrom&To=DIDto&Body=goliath$ $ in $: 
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • Thanks Matt! When I attempted `'Body='$smsbody'`, curl literally sent the string `$smsbody` (literally, not expanding the variable). I'll try adding the escape character '\' before the space characters using `sed` to the string as you suggest and see if that works! – brandeded Oct 03 '11 at 15:49
  • 1
    I just did an edit, so make sure you are up to date with my answer. Good luck! – Matt Williamson Oct 03 '11 at 15:53
  • This worked! Thanks very much! curl was only able to expand the variable within curl when escaping the space characters. I briefly got stuck trying to pipe twice instead of setting an additional bash variable. I'll work on this separately. For now I'll just use the conditional `&&` to (maybe) execute both commands in one line. – brandeded Oct 03 '11 at 16:16
  • using xargs: `smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)'} | xargs -0 -I {} echo {} | sed 's/\\$/\\\\$/g' )"` While working this out, I kept missing the double-quotes in the curl POST. Very important. – brandeded Oct 03 '11 at 16:54
  • 1
    And... the holy grail, all of this in a single line: `echo $(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)'} | xargs -0 -I {} echo {} | sed 's/\\$/\\\\$/g') | xargs -0 -I {} curl -X POST 'localhost:5000' -d 'From=DIDfrom' -d 'To=DIDto' -d "Body={}" -u SECGUID` Who needs a script! – brandeded Oct 03 '11 at 17:00
  • oops, looks like there can be problems with the above syntax if $SERVICEOUTPUT$ contains '\', so instead of `echo $(echo` use `echo $(printf "%b"` which will escape all the '\' characters. Otherwise, another sed parsing will do the trick. – brandeded Oct 03 '11 at 18:11
  • This will be the last update. I want to note that I'm having some trouble having the nagios macros expand using the single line two comments above. Beware. – brandeded Oct 04 '11 at 15:04