Ganga
Ganga

Reputation: 923

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"}

Upvotes: 0

Views: 3252

Answers (4)

Mafei
Mafei

Reputation: 3803

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

Upvotes: 0

Ganga
Ganga

Reputation: 923

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

Upvotes: 0

Alexis.M
Alexis.M

Reputation: 9

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.

Upvotes: 1

anomaaly
anomaaly

Reputation: 841

Try this

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

Upvotes: 1

Related Questions