Using cURL with a username and password?
Solution 1:
Use the -u
flag to include a username, and curl will prompt for a password:
curl -u username http://example.com
You can also include the password in the command, but then your password will be visible in bash history:
curl -u username:password http://example.com
Solution 2:
It is safer to do:
curl --netrc-file my-password-file http://example.com
...as passing a 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:
- Machine name must not include
https://
or similar! Just the hostname. - The words '
machine
', 'login
', and 'password
' are just keywords; the actual information is the stuff after those keywords.
Solution 3:
Or the same thing but different syntax
curl http://username:[email protected]/test/blah?something=123
Solution 4:
You can also just send the user name by writing:
curl -u USERNAME http://server.example
Curl will then ask you for the password, and the password will not be visible on the screen (or if you need to copy/paste the command).
Solution 5:
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"