user3293338
user3293338

Reputation: 491

how to use curl command line to access a web page with HTTP Basic Authentication

I used to use curl command in terminal to access a php web page to test some APIs in the web page. It works fine.

For example:

curl www.somesite.com -d parmetername=value 

Now this page has basic http authentication. I knew I just need to add -u to give the username and password. I also googled and almost everyone suggests to do the same as following:

curl -u username:password www.somesite.com -d parmetername=value
curl --user username:password www.somesite.com -d parmetername=value 

or

curl http://username:password@www.somesite.com -d parmetername=value

I have tried both of them, none of them works. I got the same error message as following:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<p>Additionally, a 401 Authorization Required
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Server at www.somesite.com Port 80</address>
</body></html>

I have tried to access the website via browsers. After I typed in username and password in the prompt window. I can access to this website.

Does anyone know what's going on here? Thank you.

Upvotes: 5

Views: 50143

Answers (1)

not2savvy
not2savvy

Reputation: 4301

When I had that problem, it turned out that the server would not accept the authentication scheme 'Basic', but curl uses 'Basic' unless told differently.

Check out curl's command line options regarding the authentication scheme. You can use --basic, --digest, --ntlm, --negotiate, and --anyauth. See curl's man pages for details.

While --anyauth or --negotiate sound like curl could negotiate the correct authentication method with the server, neither of them worked for me. Instead, I had to explicity use --digest, like so:

curl --digest -u username:password -d parm1=value1&parm2=value2&submit=true https://www.somesite.com 

Upvotes: 2

Related Questions