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

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

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

我该怎么做呢?


当前回答

如果你在一个有Gnome密匙环应用程序的系统上,避免直接暴露密码的解决方案是使用gkeyring.py从密匙环中提取密码:

server=server.example.com
file=path/to/my/file
user=my_user_name
pass=$(gkeyring.py -k login -tnetwork -p user=$user,server=$server -1)

curl -u $user:$pass ftps://$server/$file -O

其他回答

curl -X GET -u username:password  {{ http://www.example.com/filename.txt }} -O

通常CURL命令指的是

curl https://example.com\?param\=ParamValue -u USERNAME:PASSWORD

如果您没有任何密码或想跳过命令提示符来要求输入密码,请将密码部分留空。

即curl https://example.com\?param\=ParamValue -u USERNAME:

在某些API中它可能不起作用(比如rabbitmq)。

还有其他选择:

curl http://username:password@example.com

curl http://admin:123456@example.com

上面的格式也可以在浏览器中使用。

你可以使用命令,

curl -u user-name -p http://www.example.com/path-to-file/file-name.ext > new-file-name.ext

HTTP密码将被触发。

参考: http://www.asempt.com/article/how-use-curl-http-password-protected-site

你也可以这样发送用户名:

curl -u USERNAME http://server.example

然后Curl将要求您输入密码,并且密码将不会在屏幕上显示(或者如果您需要复制/粘贴该命令)。