我想访问一个需要用户名/密码的URL。我想用curl访问它。现在我正在做的事情是:

curl http://api.somesite.com/test/blah?something=123

我得到一个错误。我想我需要在上面的命令中指定用户名和密码。

我该怎么做呢?


当前回答

很简单,做以下几点:

curl -X GET/POST/PUT <URL> -u username:password

其他回答

很简单,做以下几点:

curl -X GET/POST/PUT <URL> -u username:password

你可以使用gpg加密的netrc文件与curl像这样:

netrc_file="$HOME/netrc.gpg"
curl --netrc-file <(gpg -d $netrc_file) https://...

使用-u标志来包含用户名,curl将提示输入密码:

curl -u username http://example.com

你也可以在命令中包含密码,但你的密码将在bash历史中可见:

curl -u username:password http://example.com

要在脚本中安全地传递密码(即防止它与ps auxf或logs一起出现),您可以使用- k -标志(从stdin读取配置)和heredoc:

curl --url url -K- <<< "--user user:password"

要让密码至少不弹出在你的.bash_history:

curl -u user:$(cat .password-file) http://example-domain.tld