Skip to content
curl

Authentication

Basic, bearer, and OAuth credentials.

By EZ4Code Team
authbasicbearer

Code

# Basic auth
curl -u user:pass https://api.example.com/secure

# Bearer token
curl -H "Authorization: Bearer TOKEN" https://api.example.com/me

# Use a .netrc file for credentials
curl -n https://api.example.com/secure

# OAuth client credentials grant
curl -X POST https://auth.example.com/token \
  -d "grant_type=client_credentials" \
  -u "clientId:clientSecret"

Explanation

The -u flag sends HTTP Basic authentication by base64-encoding user:pass, while Bearer tokens are set manually through an Authorization header. The -n flag reads credentials from a .netrc file so secrets stay out of shell history. For OAuth, exchange client credentials for a token, then call APIs with that token as a Bearer header.

More curl Snippets