简体   繁体   English

脚本中的bash curl用户名凭据

[英]bash curl username credentials within script

I am passing username and password to the curl command to receive the output from the api call.我将用户名和密码传递给 curl 命令以接收来自 api 调用的输出。 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.不确定出了什么问题,但是 curl 命令可以从命令行运行,但是当我在 bash 脚本中使用相同的 curl 命令时,它没有正确获取凭据。 API call fails with an authorization error. API 调用因授权错误而失败。 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 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无论出于何种原因,在我修改脚本以将用户名和密码作为单独的变量并传递给 curl 命令之后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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