简体   繁体   中英

bash curl username credentials within script

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM