Bash: Curl multiple lines, write output to file - bash

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

Related

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

Executing curl command with variables in bash

I am using AWX curl for update template in bash script. Somehow this curl command is not able to run
curl --insecure -v -X PATCH https://somedomain.corp.com/api/v2/job_templates/\"$test_2_template\"/ -H 'Authorization: Basic keys==' -H 'Content-Type: application/json' -d '{"extra_vars":"{\"module_name\": \"$module_name\", \"env\": \"$appenv\", \"host_group_name\": \"$host_group_name\", \"rolename\": \"$rolename\", \"app_artifact_url\": \"$app_artifact_url\"}"}'
Looks like there is some issue in passing variables to curl command via bash
However if i run this from my terminal , it's working fine.
curl --insecure -v -X PATCH https://somedomain.com/api/v2/job_templates/279/ -H 'Authorization: Basic keys==' -H 'Content-Type: application/json' -d '{"extra_vars":"{\"module_name\": \"myservice\", \"env\": \"test\", \"host_group_name\": \"env2\", \"rolename\": \"myservice\", \"artifact_version\": \"2.1.0-SNAPSHOT\"}"}'
Please suggest some solution.
You can't substitute variables inside a single quoted string.
For terrible long commands like this, breaking them up into pieces is essential for readability and maintainability:
data=$(printf \
'{"extra_vars": "{\"module_name\": \"%s\", \"env\": \"%s\", \"host_group_name\": \"%s\", \"rolename\": \"%s\", \"app_artifact_url\": \"%s\"}"}' \
"$module_name" "$appenv" "$host_group_name" "$rolename" "$app_artifact_url"
)
curl_args=(
--insecure
-v
-X PATCH
-H 'Authorization: Basic keys=='
-H 'Content-Type: application/json'
-d "$data"
)
url="https://somedomain.example.com/api/v2/job_templates/$test_2_template/"
curl "${curl_args[#]}" "$url"

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

curl command has ! character results auth failure

I am getting authentication failure error while using curl command in bash shell which password has ! char.
Below is curl command i am trying.
curl -D- -u 'some_user:somename#2019!' -X POST --data #data.txt -H 'Content-Type: application/json' https://help.myjira.com/rest/api/2/issue
I have also tried escaping ! char using \, as below, but no luck.
curl -D- -u 'some_user:somename#2019''\!' -X POST --data #data.txt -H 'Content-Type: application/json' https://help.myjira.com/rest/api/2/issue
can any one suggest.
You can try with entity code for ! = %21 (and # = %40). So:
curl -D- -u 'some_user:somename%402019%21' -X POST --data #data.txt -H 'Content-Type: application/json' https://help.myjira.com/rest/api/2/issue

Get curl http code

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

Resources