Extending a variable with spaces to a POST variable

Some studies have found some useful stackexchange articles, namely an extension variable in CURL , but this answer does not seem to handle bash variables that have spaces in them correctly.

I set the variable to awk output by parsing a substring string (actually trimming to 150 characters). The line I'm trying to do POST through curl contains spaces.

When I use the following curl arguments, the POST Body variable is set to the portion of the line 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 set as:

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

So the only part of smsbody that is POSTED is $HOSTNAME$ (which is a string without any spaces).

What is the curl syntax that I should use to properly place the bash variable for the extension, but be taken as a single data field?

It seems pretty trivial, but I accidentally messed up the quotes without any luck. I believe that someone with the best CLI-fu can handle it in a second.

Thanks!

+4
source share
1 answer

It looks like you have an extra single quote in front of the body. You also need double quotes, otherwise $ smsbody will not be evaluated.

Try the following:

 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 $ is still a problem (I don't think it's spaces), try adding \ 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 that the smsbody variable is coming in correctly.

 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 $: 
+3
source

All Articles