我想访问一个需要用户名/密码的URL。我想用curl访问它。现在我正在做的事情是:
curl http://api.somesite.com/test/blah?something=123
我得到一个错误。我想我需要在上面的命令中指定用户名和密码。
我该怎么做呢?
我想访问一个需要用户名/密码的URL。我想用curl访问它。现在我正在做的事情是:
curl http://api.somesite.com/test/blah?something=123
我得到一个错误。我想我需要在上面的命令中指定用户名和密码。
我该怎么做呢?
当前回答
或者同样的东西,但是语法不同
curl http://username:password@api.somesite.com/test/blah?something=123
其他回答
要在脚本中安全地传递密码(即防止它与ps auxf或logs一起出现),您可以使用- k -标志(从stdin读取配置)和heredoc:
curl --url url -K- <<< "--user user:password"
更安全的做法是:
curl --netrc-file my-password-file http://example.com
...由于在命令行上传递普通的用户/密码字符串,这是一个坏主意。
密码文件的格式为(按照man curl):
machine <example.com> login <username> password <password>
注意:
机器名不能包含https://或类似!只有主机名。 “machine”、“login”和“password”只是关键词;真正的信息是那些关键字之后的东西。
要让密码至少不弹出在你的.bash_history:
curl -u user:$(cat .password-file) http://example-domain.tld
你可以使用gpg加密的netrc文件与curl像这样:
netrc_file="$HOME/netrc.gpg"
curl --netrc-file <(gpg -d $netrc_file) https://...
通常CURL命令指的是
curl https://example.com\?param\=ParamValue -u USERNAME:PASSWORD
如果您没有任何密码或想跳过命令提示符来要求输入密码,请将密码部分留空。
即curl https://example.com\?param\=ParamValue -u USERNAME: