Get curl http code - bash

I have the following cURL request. I want to get the http_code, but I want in a different variable, because otherwise it messes with parsing the JSON response from the GET call.
Is there anyway to do this?
curl --write-out %{http_code} --silent --output GET --header "Accept: application/json" --header "URL"

Just use command substitution to store status code in a variable:
status=$(curl --write-out %{http_code} --silent --output tmp.out GET --header "Accept: application/json" --header "URL")
data=$(<tmp.out)
# check status now
declare -p status
# check data
declare -p data

curl -i -H "Accept: application/json" "server:5050/a/c/getName{"param0":"Arvind "}"

curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][0-9][0-9]'

response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource") For response just try $response

Try this following may be this could help you to find the solution https://gist.github.com/sgykfjsm/1dd9a8eee1f70a7068c9

Related

How to use Bash command line to curl an API with token and payload as parameters

I am novice and first timer to bash. Trying to run bash under command line to invoke an API, by passing token and payload received from two different APIs and are set as parameters. Below is my command. I am trying to add this bash script to a task in AzureBatch service job.
It has 3 curl requests,
First one (Line#1 in the code snippet below)- gets payload by
calling an API. ---- This is working fine, I am able to verify the
payload using the echo statement following the first curl command.
Second one(Line#3 in the code snippet below) - gets token by
calling the token provider ----- This is working fine as well,
verified using the echo statement.
Third one (Line#5 in the code snippet below),This is the problematic command. I am trying to pass the token and payload received from the above two commands and the curl is not able to resolve them.
both token and payload or not resolving to their values..
My Bash COmmand
/bin/bash -c
"payload=$(curl --location --request GET 'http://url/OutreachData')
&& echo -e \"The value of payload is: "'$payload'"\"
&& token=$(curl --location --request POST 'https://login.microsoftonline.com/<<tenantId>>/oauth2/v2.0/token' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'client_id=<<clientId>>' --data-urlencode 'scope=api://<<applicationId>>/.default' --data-urlencode 'client_secret=<<clientSecret>>' --data-urlencode 'grant_type=client_credentials' --data-urlencode 'Audience=api://<<applicationId>>'|jq -j '.access_token')
&& echo -e \"value of token is "'$token'"\n\"
&& result=$(curl --location --request POST 'https://url/api/<<Resource>>' --header 'accept: */*' --header 'Content-Type: application/json' --header 'Authorization: Bearer '"'$token'" --data-raw "'$payload'")
&& echo -e \"Result is "'$result'"\""
This is how the third Curl is resolving to, payload and token are not getting replaced as we can see in the authorization header and data-raw elements
++ curl --location --request POST https://url/api/ --header 'accept: /' --header 'Content-Type: application/json' --header 'Authorization: Bearer ' --data-raw ''''''''
There should be no need to explicitly run this with bash -c unless you are in a very constrained environment where you simply cannot run Bash by any other means.
The immediate problem is that code like
bash -c "echo "'$token'" && true"
ends up with $token being single-quoted in the shell which you run bash -c from. But the blazingly obvious fix is to not have this complex quoting in the first place.
payload=$(curl --location --request GET 'http://url/OutreachData')
echo "The value of payload is: '$payload'"
token=$(curl --location --request POST \
'https://login.microsoftonline.com/<<tenantId>>/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=<<clientId>>' \
--data-urlencode 'scope=api://<<applicationId>>/.default' \
--data-urlencode 'client_secret=<<clientSecret>>' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'Audience=api://<<applicationId>>' |
jq -j '.access_token')
echo "value of token is "'$token'"
result=$(curl --location --request POST \
'https://url/api/<<Resource>>' --header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer '"$token" \
--data-raw "$payload")
echo "Result is "'$result'"
If your current shell is not Bash and you need these commands to be run in Bash, a simpler workaround is to put the script in a here document, which drastically simplifies the quoting needs (or if this is in an interactive session, just run bash and run these commands at the interactive Bash prompt, then exit when you no longer want to be in Bash).

How to get curl response code in a variable along with output as a file, multiple outputs needs to be appended

I am trying to get the response code of the curl command to a variable and output of the same curl command in a file. Another curl command within a different function will run and the output should append to the same output file.
response=$(curl -i -H "content-type: application/json" -w "%{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}' >> out.txt
This is giving me neither the response nor the curl output in the file.
Below is working and gives me response as the desired http code though.
response=$(curl -i -H "content-type: application/json" -w "{http_code}" -u "$id:$cred" -H "Accept: application/json" -X POST http://sitename -d '{'<input>'}'
Echo $response

Kill a YARN application through REST api return Unauthorized by remote user dr.who

curl -u hadoop:123456 -H "Accept: application/json" -H "Content-type: application/json" -v -X PUT -d '{"state": "KILLED"}' "http://host:8088/ws/v1/cluster/apps/application_1575020200992_1673895/state"
I was already add the username and password, but return:
Unauthorized attempt to kill appid application_1575020200992_1673895 by remote user dr.who
What's dr.who ?
I figure out... add the user.name query string parameter.
like this:
curl -u hadoop:123456 -H "Accept: application/json" -H "Content-type: application/json" -v -X PUT -d '{"state": "KILLED"}' "http://host:8088/ws/v1/cluster/apps/application_1575020200992_1673895/state?user.name=hadoop"
http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/HttpAuthentication.html

How to set Quiet option for Elastic Search when doing a Bulk Insert?

I can do this:
curl -s -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json
But this fails:
curl -s -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json --quiet
How to set 'quiet'?
Thanks.
Seems you want to Disallows non-log STDOUT output with --quiet. Let's try this way-
curl -s --quiet -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json
According to the doc of --quiet,
This flag must come before any command.
If it doesn't do the job then you can use the -o switch and send the output to dev/null instead of using --quiet
curl -s -o /dev/null -XPOST 1.2.3.4:9200/my_index/my_index_type/_bulk -H "Content-Type: application/x-ndjson" --data-binary #/home/modified.json

Bash: Curl multiple lines, write output to file

I have a simple bash script that call cURL with several params.
I need to write the output in a file (also overwriting). But I cannot do.
The call itself works, but I have only an empty file (and the answer is a json, I can read on the shell with that echo)
Thank you in advance for your help
1st try
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'
>> id.txt
echo
2nd try
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'
o id.txt
echo
curl -X POST "https://www.example.com"\
-H "X-Auth-Email: $email"\
-H "X-Auth-Key: $auth_key"\
-H "Content-Type: application/json"\
--data '{"name":"'$name'","surname":"'$surname'"}'\
-o id.txt

Resources