PJ Palomaki PJ Palomaki - 2 months ago 38
Bash Question

Running Curl in Run Shell Script with arguments in Automator

I'm trying to send a curl command in Automator via the 'Run Shell Script', with arguments, but having no luck. I'm using /bin/bash and passing info as arguments. Here is my script but keep getting

Bad Request
from IFTTT. I get it's to do with not using the args correctly (if I just put
"value1":"test"
it works fine), how should I format the $1?

for f
do
curl -X POST -H "Content-Type: application/json" -d '{"value1":$1}' https://maker.ifttt.com/trigger/Automator/with/key/heremykey
done


Thanks!

Answer

You should pass a valid JSON. There is no built-in JSON support in Bash, so you need to use external tools, such as PHP, or Node:

#!/bin/bash -

function json_encode {
  printf "$1" | php -r 'echo json_encode(stream_get_contents(STDIN));'
}

for f
do
  value=`json_encode "$f"`
  curl -X POST -H "Content-Type: application/json" -d "{\"value1\":$value}" \
    https://maker.ifttt.com/trigger/Automator/with/key/heremykey
done

The script is supposed to send {"value1": ...} string for each item in $@(because the short version of the for loop operates on $@).

Comments