Running Curl in Run Shell Script with arguments in Automator - bash

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!

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 $#).

Related

Api curl script is not showing output in my unix terminal

I wrote a curl script to pull data from my api url into my unix terminal. But when I run the script below, I get a blank space as my output, rather than data from the api url. I don't get any standard error message, just a blank space followed by $ to enter a new command.
#!/bin/bash
INSTANCE_NAME="https://servicenow.com/ServiceNowData/tickets?ticks=4320000"
DATA_OUTPUT=$(curl -s -k -X GET -H "accept: application/json" $INSTANCE_NAME)
echo $DATA_OUTPUT
Page 404. And -H "Accept-Encoding: application/json"

"Unexpected end of JSON input" error when trying to do curl POST command [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I am having issues with sending the proper json data in a curl -X POST command. I have successfully run the POST command locally on my mac by copy and pasting the hardcoded values in but I am now trying to create a .sh script to automate this process. Upon running the code below I get this error:
{"message":"Unexpected end of JSON input"}
Here is the output json from JSON_STRING with names made generic and nothing else changed:
{ "url": "api_url", "tileset": "username.filename" }
Once I can figure out how to properly format the json in the POST command I know it will work, but I can't seem to get the syntax right. Hoping a set of fresh/experience bash eyes will be able to catch my mistake:). Also, all variables that I have are correct and already been confirmed by running variable values in mac terminal. Thanks in advance for the help!
curl_url="http://${bucket}.s3.amazonaws.com/${key}"
echo $curl_url
tileset_id="username.filename"
JSON_STRING=$(jq -n \
--arg bn "$curl_url" \
--arg on "$tileset_id" \
'{url: $bn, tileset: $on}')
echo $JSON_STRING
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d $JSON_STRING 'apiurl'
Absolutely required to quote the shell variable:
curl ... -d "$JSON_STRING" http://example.com/end/point
Otherwise, the shell will do word splitting, and the argument to -d becomes just {"url":
as a side note, bash arrays can help with the readability:
curl_url="http://${bucket}.s3.amazonaws.com/${key}"
tileset_id="username.filename"
JSON_STRING=$(
jq -n \
--arg bn "$curl_url" \
--arg on "$tileset_id" \
'{url: $bn, tileset: $on}'
)
curl_opts=(
-X POST
-H "Content-Type: application/json"
-H "Cache-Control: no-cache"
-d "$JSON_STRING"
)
curl "${curl_opts[#]}" 'apiurl'

passing execute shell data between build steps in jenkins

Have 2 different execute shells in jenkins and need to pass a variable from one to another
Code below shows what i have tried
Shell script #1
export storedBanner=$(curl http://my.network:8080/boardmessage | jq -r .[0].message|sed 's/<[^>]*>//g')
echo $storedBanner > ~/stored.txt
curl -i -X POST -H "Content-Type: application/json" -d "{\"message\":\"<h3>Test message<h3>\"}" http://my.network:8080/boardmessage
Shell script #2
export storedBanner= $(cat ~/stored.txt)
curl -X POST -H "Content-Type: application/json" -d "{\"message\":\"<h4>${storedBanner}<h4>\"}" http://my.network:8080/boardmessage
I want that the exported message to be stored and passed down to 2nd shell script however this doesnt seem to work.
you can use a global variable to do that
Thanks for help, I figured it out with the file and it turns out i used the wrong syntax
Putting this in the shell script #2 worked :)
storedBanner=$(<banner.txt)

POST multiple files with -d in curl

I'm using curl to create several classifications. I have written the json for the many classifications and they are in one folder. I would like to create all the classifications in one go. But using curl I can only create them one at a time. How could I make them in one request?
curl -u admin:admin -H "Content-Type: application/json" -X POST -d #pii.json http://127.0.0.1:21000/api/atlas/v2/types/typedefs
The curl manual for -d says 'Multiple files can also be specified'. How can I do this? All my attempts have failed.
Do I need a bash script instead? If so, could you help me - I'm not a coder and I'm struggling without an example!
Thanks in advance.
You probably don't want to use multiple -d with JSON data since curl concatenates multiple ones with a & in between. As described in the man page for -d/--data:
If any of these options is used more than once on the same command
line, the data pieces specified will be merged together with a
separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would
generate a post chunk that looks like 'name=daniel&skill=lousy'.
You can however easily and conveniently pass several files on stdin to let curl use them all in one go:
cat a.json b.json c.json | curl -d#- -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
(please note that -X POST has no place on a command line that uses -d)
I found the following to work in the end:
<fileToUpload.dat xargs -I % curl -X POST -T "{%}" -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs
Where fileToUpload.dat contained a list of the .json files.
This seemed to work over Daniel's answer, probably due to the contents of the files. Hopefully this is useful to others if Daniel's solution doesn't work for them.
I needed to upload all the *.json files from a folder via curl and I made this little script.
nfiles=*.json
echo "Enter user:"
read user
echo "Enter password:"
read -s password
for file in $nfiles
do
echo -e "\n----$file----"
curl --user $user:$password -i -X POST "https://foo.bar/foo/bar" -H "Content-Type: application/json" -d "#$file"
done
Maybe fits your needs.

how to query results from a command in a bash script

I have the following bash script:
#!/bin/bash
echo "GET load test"
for i in $(seq 1 50) do
MY_RESULTS = "$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123)"
echo "$MY_RESULTS"
done
I'm not sure why but the results don't print out. When I change the code to the following, it works:
curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123)"
Then the results just display on the screen. Ultimately, I'd like to search the contents of the results for a particular string value like "200 OK"
I created another test script to test how to save, and echo values out to the screen and it works. It look like this:
MY_VAR="$(find -name loadtest.sh)"
echo "$MY_VAR"
The results it displays is:
./loadtest.sh
I can't see what's different about this variable called MY_VAR and the one in my load test
In bash, you can not have whitespace about the equals operator in an assignment statement. Remove the whitespace when assigning $MY_RESULT:
MY_RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123)"

Resources