Bash CURL Script Works Without Variable But Not With One

Hi, all. Long-time Keyboard Maestro user here, but first time poster. I have tried to research this issue on this forum and elsewhere online. I am stumped. Hoping someone can help.

I’m having a problem using Keyboard Maestro to send JSON to the Zendesk API to add users to groups. I’m doing it via CURL in a Bash shell script. Before you assume this is a Zendesk API issue, bear with me! I really think it’s KM or something stupid I’m doing in KM. (Probably the latter!)

A Bash script like this run from KM works perfectly:

curl https://yoursite.zendesk.com/api/v2/group_memberships/create_many.json
-X POST -d ‘{“group_memberships”: [{“user_id”: 123, “group_id”: 1}, {“user_id”: 456, “group_id”: 1}, {“user_id”: 789, “group_id”: 1}]}’
-H “Content-Type: application/json” -v -u (email address here):(password here)

But, I want to pass user_id and group_id info to the bash script via a KM variable ($KMVAR_CURL_Text).

The $KMVAR_CURL_Text variable contents match the user_id and group_id info I have the manual script above exactly. I have run the variable text through JSON validators. It’s fine. I have manually pasted the variable data back into the bash script, It’s fine.

But, if I use the variable $KMVAR_CURL_Text:

‘{“group_memberships”: [{“user_id”: 123, “group_id”: 1}, {“user_id”: 456, “group_id”: 1}, {“user_id”: 789, “group_id”: 1}]}’

with the shell script:

curl https://yoursite.zendesk.com/api/v2/group_memberships/create_many.json
-X POST -d $KMVAR_CURL_Text
-H “Content-Type: application/json” -v -u (email address here):(password here)

Zendesk’s API tells me:

< HTTP/1.1 400 Bad Request
< Server: nginx
< Date: Mon, 21 Sep 2015 15:09:21 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 111
< Connection: keep-alive
< Status: 400 Bad Request
< X-Zendesk-API-Version: v2
< X-Zendesk-API-Warn: Removed restricted keys ["’{“group_memberships”:"] from parameters according to whitelist

I’m happy to take this up with Zendesk. But, since I can make this work manually, I feel like the issue is with me or KM. Anyone have any idea what I’m doing wrong? Something with escaping, non-escaping, or text encoding related to the variable maybe?

Thanks!

Marc :slight_smile:

Try wrapping KMVAR_CURL_Text in quotes and {brackets} like so:

curl https://yoursite.zendesk.com/api/v2/group_memberships/create_many.json \
	-X POST \
	-d "${KMVAR_CURL_Text}" \  
	-H "Content-Type: application/json" \
	-v \
	-u (email address here):(password here)

Once you get it working, you might want to consider replacing the -v with -sfL (silent, fail, Location)

Also: Be sure that you are testing curl's exit code to see if it succeeded or not.

So, for example, once you have it working, it should look something like:

curl -sfL 'https://yoursite.zendesk.com/api/v2/group_memberships/create_many.json' \
	-X POST \
	-d "${KMVAR_CURL_Text}" \  
	-H "Content-Type: application/json" \
	-u "(email address here):(password here)"

EXIT="$?"

if [ "$EXIT" != "0" ]
then
	
	echo "curl failed with exit code = $EXIT"

	exit 1
fi

Notice that I put “quotes” around the email/password pair as well.

HTH

2 Likes

Perfect answer! Thanks so much!