Using cURL with a username and password?

2019-01-04 05:14发布

I want to access a URL which requires a username/password. I'd like to try accessing it with curl. Right now I'm doing something like:

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

I get an error. I guess I need to specify a username and password along with the above command.

How can I do that?

13条回答
▲ chillily
2楼-- · 2019-01-04 05:41

To let the password least not pop up in your .bash_history:

curl -u user:$(cat .password-file) http://example-domain.tld
查看更多
孤傲高冷的网名
3楼-- · 2019-01-04 05:42

It is safer to do:

curl --netrc-file my-password-file http://example.com

...as passing plain user/password string on the command line is a bad idea.

The format of the password file is (as per man curl):

machine <example.com> login <username> password <password>

Note:

  1. Machine name must not include https:// or similar! Just the hostname.
  2. The words 'machine', 'login', and 'password' are just keywords; the actual information is the stuff after those keywords.
查看更多
对你真心纯属浪费
4楼-- · 2019-01-04 05:46

To securely pass the password in a script (i.e. prevent it from showing up with ps auxf or logs) you can do it with the -K- flag (read config from stdin) and a heredoc:

curl --url url -K- <<< "--user user:password"
查看更多
Evening l夕情丶
5楼-- · 2019-01-04 05:49

If you are on a system that has Gnome keyring app a solution that avoids exposing the password directly is to use gkeyring.py to extract the password from the keyring:

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
查看更多
再贱就再见
6楼-- · 2019-01-04 05:49

You can use command like,

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

Then HTTP password will be triggered.

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

查看更多
爷的心禁止访问
7楼-- · 2019-01-04 05:49

pretty easy, do the below:

curl -X GET/POST/PUT <URL> -u username:password
查看更多
登录 后发表回答