Skip to content
On this page

To send HTTP Basic Authentication with curl, use the -u (or --user) flag: curl -u username:password https://example.com. curl Base64-encodes the username:password pair and adds an Authorization: Basic <token> header for you. That's the whole job — the rest is handling special characters, hiding the password from your shell history, and remembering that Basic Auth is only safe over HTTPS.

Quick facts

Flag-u / --user
Syntaxcurl -u user:pass https://example.com
What it doesBase64-encodes user:pass into an Authorization: Basic header
Manual header-H "Authorization: Basic <base64>"
SecurityOnly over HTTPS — Basic Auth is encoded, not encrypted

The -u flag (the normal way)

Pass the credentials as username:password after -u:

curl -u myuser:mypassword https://example.com/api/data

curl encodes them and sends Authorization: Basic bXl1c2VyOm15cGFzc3dvcmQ=. --user is the long form of -u and behaves identically. Basic is curl's default auth scheme, so you don't need --basic.

Hide the password from your shell history. Give only the username and curl will prompt for the password interactively (it won't be echoed or stored):

curl -u myuser https://example.com/api/data
# Enter host password for user 'myuser':

Setting the Authorization header manually

If you already have the Base64 token (or want full control), skip -u and send the header yourself with -H:

# Build the token, then send it
TOKEN=$(printf 'myuser:mypassword' | base64)
curl -H "Authorization: Basic $TOKEN" https://example.com/api/data

This is exactly what -u produces — useful when the token is supplied by another system, or when you're translating a curl command into application code.

Special characters and spaces

If the username or password contains shell-special characters ($, !, spaces, @), wrap the pair in single quotes so your shell doesn't expand them:

curl -u 'new_user:my$ecret p@ss!' https://example.com

If the password itself contains a colon, only the first colon is treated as the separator, so a colon in the password is fine — but a colon in the username is not allowed by the Basic Auth spec.

Reusing credentials with .netrc

To keep credentials out of the command line entirely, store them in a ~/.netrc file and let curl read them with -n / --netrc:

# ~/.netrc  (chmod 600 it)
machine example.com login myuser password mypassword
curl -n https://example.com/api/data
# or point at a specific file:
curl --netrc-file ./secrets.netrc https://example.com/api/data

Security: only over HTTPS

Basic Auth is encoded, not encrypted
The Base64 token is trivially reversible. Over plain http:// anyone on the network can read your password. Always use an https:// endpoint so TLS encrypts the header in transit.

For scraping behind a login, Basic Auth is rarely the blocker — anti-bot systems care about your IP reputation and browser fingerprint, not your auth header. If an authenticated endpoint returns 403 or 429 even with correct credentials, the fix is proxies and a real browser profile, not the auth flag.

Code example

bash
# 1) Simplest: -u user:password (curl adds the Authorization header)
curl -u myuser:mypassword https://example.com/api/data

# 2) Prompt for the password (keeps it out of shell history)
curl -u myuser https://example.com/api/data

# 3) Send the header yourself
curl -H "Authorization: Basic $(printf 'myuser:mypassword' | base64)" \
     https://example.com/api/data

# 4) Special characters -> single-quote the pair
curl -u 'new_user:my$ecret p@ss!' https://example.com/api/data

Next in Web fundamentals for scrapers · 6 of 7

Sending structured payloads from the same tool.

How to Send and Receive JSON with curl

Related terms

Concept map

Concept map

How How to Use Basic Auth with curl connects

The terms most directly tied to this one. Hover a node to see its neighbours, click to preview, drag to rearrange.

0 terms · 0 connections
You are here · Web Technologies
Building map…

Frequently asked questions

How do I send basic auth with curl?

Use the -u (or --user) flag: curl -u username:password https://example.com. curl Base64-encodes the pair and adds an Authorization: Basic header automatically. Use HTTPS so the credentials are encrypted in transit.

How do I set the Authorization header manually in curl?

Build the Base64 token and pass it with -H: curl -H "Authorization: Basic $(printf 'user:pass' | base64)" https://example.com. This produces the same header that -u would generate.

How do I handle special characters in a curl password?

Wrap the user:password pair in single quotes so the shell does not expand characters like $, !, or spaces: curl -u 'user:my$pass!' https://example.com. A colon is allowed in the password (only the first colon separates user from password) but not in the username.

Is curl basic auth secure?

Only over HTTPS. Basic Auth Base64-encodes the credentials, which is not encryption — over plain HTTP they can be read on the network. Always use an https:// endpoint, and prefer .netrc or a password prompt over putting the password directly in the command.

Last updated: 2026-06-08