0

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

5
  • 2
    USERNAME != USER ? however, PASSWORD=PASSWORD ;-) If that solves your problem, please delete this Q as misspellings in code are off-topic. Good luck. Commented Oct 31, 2017 at 3:57
  • 2
    Also, $USER is a reserved variable name (automatically set to the current username). It's best to use lowercase (or mixed-case) variable names to avoid conflicts like this. Commented Oct 31, 2017 at 4:01
  • @shellter, I edit the question to avoid confusions. Thanks Commented Oct 31, 2017 at 4:08
  • @GordonDavisson, thats a good point, I changed the username to mixedcase and noticed the same error again. I have updated the question with the error stacktrace Commented Oct 31, 2017 at 4:12
  • 1
    With that fixed, it should work. Are you properly quoting the arguments to the script? Also, try adding set -x ti the script just before the curl command, so it'll print the equivalent of what it's actually running (note: what it prints isn't literally what's being executed, it's something equivalent to it, but sometimes with strange quoting or escaping of arguments). Commented Oct 31, 2017 at 6:27

4 Answers 4

1

Try this

UNAME=$1
PASSWORD=$2
curl --silent -u "${UNAME}:${PASSWORD}" https://example.com/api/check
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

1 Comment

I would not recommend doing this. Putting the command in a variable shouldn't solve anything, and will cause trouble if e.g. the password contains space or anything in $IFS (or maybe some shell wildcard characters or... See BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!). If this appears to fix the problem, it's likely there's actually something else going on that you haven't understood yet and it may reappear later. Switch to lowercase variables, try set -x, and find out what's really happening.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.