# curl Basic Auth: -u, Header, .netrc, Proxy

> Use basic auth with curl: -u user:password, the Authorization header it builds, special characters, .netrc, digest, and site login next to proxy login (-U).

[Home](https://quanticdata.io/) / [Blog](https://quanticdata.io/blog/) / curl Basic Auth: -u, Header, .netrc, Proxy

# curl basic auth: -u user:password, the Authorization header and proxy logins

Proxies · Nov 20, 2026 · 5 min read · By [Aldo Morese](https://quanticdata.io/about/), founder of QuanticData

![How curl sends basic authentication: -u ada:s3cret becomes the header Authorization: Basic YWRhOnMzY3JldA==, which is only the base64 of ada:s3cret; the server answers 200 with authenticated true, or 401 with WWW-Authenticate when the password is wrong; the card shows that the site login goes in -u and the proxy login in -U.](https://quanticdata.io/blog/curl-basic-auth/hero.webp?v=20261120h)

On this page

- [Basic auth with -u](https://quanticdata.io/blog/curl-basic-auth/#basic-auth-with-u)
- [The header curl sends, and building it by hand](https://quanticdata.io/blog/curl-basic-auth/#the-header-curl-sends-and-building-it-by-hand)
- [Wrong password, no password: 401](https://quanticdata.io/blog/curl-basic-auth/#wrong-password-no-password-401)
- [Special characters in the password](https://quanticdata.io/blog/curl-basic-auth/#special-characters-in-the-password)
- [Keeping the password out of the command: .netrc](https://quanticdata.io/blog/curl-basic-auth/#keeping-the-password-out-of-the-command-netrc)
- [Digest and --anyauth](https://quanticdata.io/blog/curl-basic-auth/#digest-and-anyauth)
- [Site login and proxy login on one command](https://quanticdata.io/blog/curl-basic-auth/#site-login-and-proxy-login-on-one-command)
- [Basic auth with a POST](https://quanticdata.io/blog/curl-basic-auth/#basic-auth-with-a-post)

To send HTTP basic authentication with curl, use `-u`: `curl -u username:password https://example.com/`. curl joins the two with a colon, base64-encodes them and sends `Authorization: Basic ...`. Give only the username (`-u username`) and curl asks for the password, so it stays out of your shell history.

**Run it on the free credit.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-lead&utm_content=curl-basic-auth) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

We ran every command below on 20 November 2026 with curl 8.22.0 on Windows 11, against httpbin.org's test endpoints, which accept a username and password you choose in the URL (here `ada` and `s3cret`), and through QuanticData proxies.

## Basic auth with -u

```bash
$ curl -u ada:s3cret https://httpbin.org/basic-auth/ada/s3cret
{
  "authenticated": true,
  "user": "ada",
  "authorized": true
}
```

`--user` is the long form of `-u`. Basic is the scheme `-u` uses unless you ask for another one, as the digest test further down shows.

## The header curl sends, and building it by hand

`-v` shows the request headers. The Authorization line is all that `-u` adds:

```bash
$ curl -sv -u ada:s3cret https://httpbin.org/basic-auth/ada/s3cret -o /dev/null 2>&1 | grep -E "^> Authorization|^< HTTP"
> Authorization: Basic YWRhOnMzY3JldA==
< HTTP/1.1 200 OK
```

That value is `ada:s3cret` in base64. You can build the same header yourself, for tools that only take headers:

```bash
$ printf 'ada:s3cret' | base64
YWRhOnMzY3JldA==
$ curl -H "Authorization: Basic YWRhOnMzY3JldA==" https://httpbin.org/basic-auth/ada/s3cret
{
  "authenticated": true,
  ...
}
```

Use `printf`, not `echo`: `echo` adds a newline, which gets encoded too (`YWRhOnMzY3JldAo=`), and the server answered 401 to that header. And because base64 decodes back to the password in one step, basic auth must only travel over https.

## Wrong password, no password: 401

| Command | Status | What else came back |
| --- | --- | --- |
| `curl -u ada:s3cret URL` | 200 | `"authenticated": true` |
| `curl -u ada:wrong URL` | 401 | `www-authenticate: Basic realm="Fake Realm"` |
| `curl URL` (no credentials) | 401 | Same challenge |
| `curl -u ada URL` | waits | curl printed `Enter host password for user 'ada':` |

curl exits 0 on a 401 unless you add `-f`, so scripts should read the status with `-w "%{http_code}"` or use `-f`. The `www-authenticate` header names the scheme the server wants; `-D - -o /dev/null` prints it:

```bash
$ curl -sS -o /dev/null -w "%{http_code}\n" -u ada:wrong https://httpbin.org/basic-auth/ada/s3cret
401
$ curl -sS -D - -o /dev/null -u ada:wrong https://httpbin.org/basic-auth/ada/s3cret | grep -i "www-authenticate"
access-control-expose-headers: Retry-After, RateLimit, RateLimit-Policy, WWW-Authenticate
www-authenticate: Basic realm="Fake Realm"
```

"Basic" in that header means `-u` as shown here is the right tool. Other servers want another scheme: on the digest endpoint further down, `-u` alone got 401 and `--digest` worked. The realm is only a label for the protected area; curl does not need it.

## Special characters in the password

| Password | Command that worked |
| --- | --- |
| `pa:ss` (a colon) | `curl -u 'ada:pa:ss' URL`: curl splits at the first colon only |
| `p@ss` with `-u` | `curl -u 'ada:p@ss' URL`: no encoding needed |
| `p@ss` inside the URL | `curl 'https://ada:p%40ss@httpbin.org/...'`: encode @ as %40 |

Put the pair in single quotes in bash so the shell leaves `$`, `!` and spaces alone. Credentials in the URL (`https://ada:s3cret@host/`) also worked, but they end up in logs and history more easily than `-u`.

**Try it on your own targets.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-inline&utm_content=curl-basic-auth) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

## Keeping the password out of the command: .netrc

```bash
$ cat netrc.txt
machine httpbin.org login ada password s3cret
$ curl --netrc-file netrc.txt https://httpbin.org/basic-auth/ada/s3cret
{
  "authenticated": true,
  ...
}
```

One line per host. `--netrc-file` reads the file you name; keep it readable only by your user. Together with the prompt of `-u ada`, it is the way to keep passwords out of scripts and shell history.

## Digest and --anyauth

Some servers ask for digest authentication instead of basic. Sending basic to a digest endpoint returned 401; `--digest` fixed it, and so did `--anyauth`, which lets curl ask the server and pick the scheme:

```bash
curl --digest -u ada:s3cret https://httpbin.org/digest-auth/auth/ada/s3cret     # authenticated: true
curl --anyauth -u ada:s3cret https://httpbin.org/digest-auth/auth/ada/s3cret    # authenticated: true
```

## Site login and proxy login on one command

Basic auth also exists one hop earlier: an authenticated proxy wants its own username and password. curl keeps the two apart: `-u` is for the site, `-U` (or `--proxy-user`) is for the proxy. We sent both at once through three QuanticData networks:

```bash
curl -x http://residentialboson.quanticdata.io:10000 -U "USER-type-residential-country-us-session-ca01-lifetime-30:PASS" -u ada:s3cret https://httpbin.org/basic-auth/ada/s3cret
curl -x http://mobile.quanticdata.io:8080 -U "USER-country-us-type-mobile-sid-ca03-ttl-30m:PASS" -u ada:s3cret https://httpbin.org/basic-auth/ada/s3cret
curl -x http://residentialpremium.quanticdata.io:8080 -U "USER-country-de-sid-ca02-ttl-30m:PASS" -u ada:s3cret https://httpbin.org/basic-auth/ada/s3cret
```

| Network | -U (proxy) | -u (site) | Result |
| --- | --- | --- | --- |
| Residential Basic, US | correct | correct | `"authenticated": true` |
| Mobile, US | correct | correct | `"authenticated": true` |
| Residential Premium, Germany | correct | correct | `"authenticated": true` |
| Residential Basic, http:// URL | wrong password | none | 407 from the proxy |
| Residential Basic, https:// URL | wrong password | none | curl: (7) CONNECT tunnel failed, response 407 |

The status code tells you which login failed: 401 comes from the site (check `-u`), 407 from the proxy (check `-U`). Our guide to [407 Proxy Authentication Required](https://quanticdata.io/blog/how-to-fix-407-proxy-authentication-required/) goes through the proxy side, and [how to use a proxy with curl](https://quanticdata.io/blog/how-to-use-a-proxy-with-curl/) covers gateways and SOCKS5.

## Basic auth with a POST

`-u` combines with any request. `curl -u ada:s3cret --json '{"a":1}' https://httpbin.org/anything` arrived as a POST with `"Authorization": "Basic YWRhOnMzY3JldA=="` and the JSON body `{"a": 1}`. The body options are in [curl POST requests](https://quanticdata.io/blog/curl-post-request/).

When your scripts call protected APIs from a set country, our [residential proxies](https://quanticdata.io/residential-proxies/) and [mobile proxies](https://quanticdata.io/mobile-proxies/) take the `-x` and `-U` options above; there is a [Residential Basic free trial](https://quanticdata.io/residential-proxy-free-trial/).

### Sources & further reading

- [curl manual: -u, --user](https://curl.se/docs/manpage.html#-u)
- [curl manual: -U, --proxy-user](https://curl.se/docs/manpage.html#-U)
- [curl manual: --netrc-file](https://curl.se/docs/manpage.html#--netrc-file)
- [RFC 7617: The Basic HTTP Authentication Scheme](https://www.rfc-editor.org/rfc/rfc7617)

## FAQ

Quick answers on curl basic auth.

[Something else? Ask us](mailto:hello@quanticdata.io)

### How do I use basic auth with curl?

Use `curl -u username:password URL`. curl base64-encodes the pair and sends it as `Authorization: Basic ...`. In our test, `-u ada:s3cret` sent `Authorization: Basic YWRhOnMzY3JldA==` and the server answered 200.

### How do I send a basic auth header in curl?

Either let `-u` build it, or send it yourself: `curl -H "Authorization: Basic $(printf 'user:pass' | base64)" URL`. Both give the same header; use printf so no newline is encoded.

### How do I make curl ask for the password?

Give only the username: `curl -u ada URL`. curl prints `Enter host password for user 'ada':` and waits, so the password never appears in the command or in your shell history.

### How do I use a password with special characters in curl?

With `-u`, quote the pair: `-u 'ada:p@ss'` worked, and so did a colon (`-u 'ada:pa:ss'`), because curl splits at the first colon. Inside a URL, percent-encode the character: `p%40ss` for `p@ss`.

### What is the difference between curl -u and -U?

`-u` logs in to the site, `-U` logs in to the proxy set with `-x`. A wrong `-u` gives 401 from the site; a wrong `-U` gives 407 from the proxy, or "CONNECT tunnel failed, response 407" (exit 7) for an https URL.

### Is curl basic auth secure?

Only over https. The header is base64, which anyone who sees it can decode back to the password. Over https it travels encrypted; over plain http it does not.

## Authenticated proxies for authenticated APIs

Residential and mobile proxies with username and password login, country targeting and sticky sessions, ready for curl -x and -U.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-end&utm_content=curl-basic-auth) · [Explore Residential Proxies from $0.80/GB](https://quanticdata.io/residential-proxies/)

## Related reading

- [curl Timeout: --max-time, --connect-timeout](https://quanticdata.io/blog/curl-timeout/): Proxies · Use --max-time (-m) to cap the whole request and --connect-timeout to cap the connection. Below: the exact "curl: (28)" lines we triggered, what curl did with no timeout at all (21 seconds on Windows), retries, stall detection with --speed-limit, and how both timeouts behave through a proxy.
- [curl Download File: -O, -o, -L and Resume](https://quanticdata.io/blog/curl-download-file/): Proxies · curl -O URL saves the file under its remote name, curl -o name URL under the name you choose, and -L follows redirects such as GitHub's. Below: every option run for real, including the 0-byte file a 302 leaves behind, a resumed download that matched the original byte for byte, and 1 MB through residential and mobile proxies.
- [curl Show Response Headers and Send Headers](https://quanticdata.io/blog/curl-headers/): Proxies · curl -i prints the response headers with the body, -I only the headers (with a HEAD request), and -D - -o /dev/null the headers of a normal GET. To send headers, use -H "Name: value", once per header. Below: all of it run for real, plus a single header with -w, removing curl's defaults, headers from a file, and what a proxy sees.

---

Source: https://quanticdata.io/blog/curl-basic-auth/ · Site index for AI: https://quanticdata.io/llms.txt
