Console9

How to send authenticated requests with curl

Send HTTP requests with basic auth, bearer tokens, and client certificates using curl.

How to send authenticated requests with curl

Send HTTP requests with basic auth, bearer tokens, and client certificates using curl.

Prerequisites

  • curl installed (version 7.0+).
  • Valid credentials for the target API or service.

Step-by-Step: Send Authenticated Requests with curl

  1. Send a request with HTTP basic authentication using the -u flag. curl encodes the username and password as a Base64 Authorization header:

    curl -u username:password https://api.example.com/users
  2. Send a request with a bearer token. curl adds the Authorization: Bearer header with -H:

    curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.com/users
  3. Send a request with a client TLS certificate for mutual authentication. curl presents the certificate and private key to the server:

    curl --cert /path/to/client.crt --key /path/to/client.key https://api.example.com/secure

How to Verify Authentication Succeeded

A successful request returns HTTP status 200. An authentication failure returns 401 (Unauthorized) or 403 (Forbidden). Check the status code with -w:

curl -o /dev/null -s -w "%{http_code}" -u user:pass https://api.example.com/users