Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cURL with a username and password?

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?

like image 867
user246114 Avatar asked Apr 07 '10 18:04

user246114


People also ask

How do I use curl username and password?

For example, if a website has protected content curl allows you to pass authentication credentials. To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.

What does -- user do in curl?

These curl recipes show you how to perform basic HTTP server authorization. To do that, use the -u user:pass command line argument. If you skip the password (but leave the colon), then no password is set. If you also skip the colon, then curl prompts for the password.

How do I open a website with curl?

The syntax for the curl command is as follows: curl [options] [URL...] In its simplest form, when invoked without any option, curl displays the specified resource to the standard output. The command will print the source code of the example.com homepage in your terminal window.

How do I change user agent in curl?

You can use the -A or --user-agent command-line option to pass your own User-Agent string to Curl. By default, Curl sends its own User-Agent string to the server in the following format: "curl/version. number".


2 Answers

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 
like image 54
Finbarr Avatar answered Oct 07 '22 18:10

Finbarr


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:

  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.
like image 36
Pierre D Avatar answered Oct 07 '22 18:10

Pierre D