curl command has ! character results auth failure - bash

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

Related

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

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

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

bash encapsulate command options in variables

I have a bunch of these to test my RESTful API
$CURL \
-v \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
$URL/$PATH/sessions
I kinda want to shorten it to something like
CURLOPTS="-v -H 'Content-Type: application/json' -H 'Accept: application/json'"
$CURL \
$CURLOPTS \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
$URL/$PATH/sessions
but the options don't seem to be passed in. Any clues?
Short answer: see BashFAQ #50:'m trying to put a command in a variable, but the complex cases always fail!.
Long answer: Putting commands (or parts of commands) into variables and then getting them back out intact is complicated. The reason your script doesn't work is because of the order in which the shell parses the command line: it parses (and removes) quotes and escapes, then replaces variable values. By the time $CURLOPTS gets replaced, it's too late for the quotes to have their intended effect; instead, they're passed to curl as part of the arguments, which confuses curl greatly.
The solution: store the options in an array rather than a plain string:
CURLOPTS=(-v -H 'Content-Type: application/json' -H 'Accept: application/json')
$CURL \
"${CURLOPTS[#]}" \
-X POST \
-d '{"user":{"email":"user#example.com","password":"secret"}}' \
"$URL/$PATH/sessions"
You can use an array and trigger word splitting
$ set -x
$ CURLOPTS=(-v -H 'Content-Type: application/json' -H 'Accept: application/json')
$ : curl "${CURLOPTS[#]}"
+ : curl -v -H 'Content-Type: application/json' -H 'Accept: application/json'

Resources