all. 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 :-)
Always quote shell expansions, unless you have a specific and compelling reason to do otherwise.
-d "$KMVAR_CURL_Text"
Otherwise, the string goes through string-splitting (becoming multiple words split on the contents of IFS
) and glob expansion (having each of those words treated as a glob expression and potentially replaced with a list of filenames).