bash curl username credentials within script - bash

I am passing username and password to the curl command to receive the output from the api call. Not sure whats wrong, but the curl command works from the command line, but when I use the same curl command within a bash script, it doesn't take the credentials properly. API call fails with an authorization error. Can someone throw some pointers here?
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
Here is the script
USERNAME=$1
PASSWORD=$2
curl --silent -u "${USERNAME}:${PASSWORD}" https://example.com/api/check
{"timestamp":1509422967185,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/api/check"}

Try this
UNAME=$1
PASSWORD=$2
curl --silent -u "${UNAME}:${PASSWORD}" https://example.com/api/check

Blockquote
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
This might be a red herring on the quotes but your script won't accept strings after the $ sign.
Might need to wrap your username & password in quotes as you give your script your inputs.

Thank you for all the answers !! For whatever reason, after i modified the script to have the username and password as separate variable and passing to the curl command worked
CREDS="${USERNAME}:${PASSWORD}"
CURL_CMD="curl --silent -u ${CREDS}"
${CURL_CMD} https://example.com/api/check

You can use like this,
example:
curl --location --request POST 'http://119.235.1.63/test' \
--header 'Content-Type: application/json' \
--data-raw '{ "CompanyId":"mafei", "Pword":"mafei123", "SmsMessage":"I am '${USER}'", "PhoneNumber": [ "712955386"] }'
in your code please use single quotes like below
curl --silent -u "'${USERNAME}:${PASSWORD}'" https://example.com/api/check

Related

Command not found while executing curl in shell script

I am executing the below curl command in shell script. But I am getting command not found error. So wanted to check if I am making any mistake while executing it.
return = $(curl -s --location --request GET --url 'https://testurl.mywebsite.com/api/accts?Id=trst_id&var=test_var' --header 'type: app/test')
echo "data is: ${return}"
I am getting the error:
return: command not found
Kindly let me know if there is a problem with the way I am trying to execute the curl command.
Thank you.
Like dan pointed out in comments, the extra whotespaces between the variable and command caused the issue. After removing extra whitespaces it worked.
return=$(curl -s --location --request GET --url 'https://testurl.mywebsite.com/api/accts?Id=trst_id&var=test_var' --header 'type: app/test')
echo "data is: ${return}"

curl PUT using auth token header to mesosphere fails without eval

EDIT:
I have managed to make it work with
response=$(
curl -k -X PUT -d "$marathon_payload" --write-out %{http_code} --silent --output "$tmp"\
-H "Authorization: token=$dcos_token" -H "$header_content_type" $app_id_url
)
The single quotes were causing the problem. It took a few gyrations but all good.
MORAL: quotes inside the value don't matter if the value is properly quoted UNLESS you eval the whole thing, and I should have known that. Occam's wins again.
end edit
I am initiating Mesosphere microservice deployments with curl, but it won't succeed without using eval. Since I recently inherited this code I've been trying to scrub the eval out of it just as a matter of habit, but it's thwarting me.
The script initiates the deployment with
response=$(
eval curl -k -X PUT -d "'$marathon_payload'" --write-out %{http_code} --silent --output $tmp\
-H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $app_id_url
)
If it gets a 200 or a 201, it loops a curl to effectively screen-scrape the deployments page till the request disappears.
chkDeploy() { rm -f $tmp;
eval curl -k -X GET --silent --write-out %{http_code} --silent --output $tmp\
-H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $deployments_url
}
response=$( chkDeploy )
$dcos_token is a base64 encoded string.
It then checks the service with another curl loop to the info page so it can verify the version number. This one is working fine with no eval.
chkCode() {
curl -k -X GET --write-out %{http_code} --silent --output $tmp $info_url;
}
response=$( chkCode )
The first two return 401, authentication failure.
I'm guessing the auth token quoting is off.
There's no reason to use eval here; you just need to quote the arguments to -H properly.
response=$(
curl -k -X PUT -d "$marathon_payload" \
--write-out %{http_code} \
--silent --output "$tmp" \
-H "Authorization: token=$dcos_token" \
-H "$header_content_type" "$app_id_url"
)

How to retrieve error code from cURL on shell

I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

For loop from file with multiple columns-BASH

i have following text file (output.txt)
TECH-746 TECH 10400
TECH-747 TECH 10400
i need to read all columns and pass it to 3 variables and then submit it to curl command. While read simple won't work with curl (don't know why) so i need to use for loop (it works with curl), can i use one for loop or need to nest multiple ones
for project in `cat output.txt`; do
echo $project
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
code above works, so i just want to "extend" to to include all other columns in file
while read can pull a line into distinct variables.
while read project col2 col3
do
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
done < sourcefile.txt
EDIT:
The curl command also misses escaping one quote, compare the two lines below, the first one is the original, the second one is the one I've corrected.
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null
curl -D- -u user:pass -X POST --data "{\"fields\":{\"project\":{\"key\":\"TECH\"},\"parent\":{\"key\":\"$project\"},\"summary\":\"test\",\"description\":\"test.\",\"issuetype\":{\"name\":\"Sub-task\"}}}" -H "Content-Type:application/json" --silent https://jira.company.com/rest/api/latest/issue/ >/dev/null

Assign curl http reponse code to variable

I've got the following call in a bash script I'm creating
response= curl -X POST $URL -u "$USER:$PASSWORD" --data-urlencode "key=$key" --data "label=pub_key" -o /dev/null --silent --write-out "%{http_code}"
I can see 200 being written to the console however $response always ends up null.
I've also tried the following but it was not better.
response= $(curl -X POST $URL -u "$USER:$PASSWORD" --data-urlencode "key=$key" --data "label=pub_key" -o /dev/null --silent --write-out "%{http_code}")
Any help for a bash noob would be appreciated.
Space -- the final frontier:
response=$(curl -X ...)
Note: no spaces around the =. The shell is white-space sensitive in a few places and variable assignments are one of them.
With the space, as in var= command args, you set var as empty in a one-shot assignment and then run command.

Resources