cURL: Add Header, Multiple Headers, Authorization

curl allows to add extra headers to HTTP requests.

The HTTP headers are used to pass additional information between the client and the server.

In this article i am showing the examples of how to add header in curl, how to add multiple headers and how to set authorization header from the Linux command line.

Cool Tip: Set User-Agent in HTTP header using cURL! Read more →

Add Header in cURL

Add header X-Forwarded-For:

$ curl -H "X-Forwarded-For:"

Set header Accept: application/xml and GET data from the server:

$ curl -H "Accept: application/xml" -X GET

Set header Content-Type: application/json and send data via POST request:

$ curl -H "Content-Type: application/json" \
       -X POST \
       -d '{"key1":"value1","key2":"value2"}' \

POST data from data.json file:

$ curl -H "Content-Type: application/json" \
       -X POST \
       -d @data.json \

Example of the data.json file content:

$ cat data.json

Set Authorization Header in cURL

Basic authentication using Username and Password:

$ curl --user <USER>:<PASSWORD>

Set header with Basic authentication token:

$ curl -H "Authorization: Basic <ACCESS_TOKEN>"

To generate the basic authentication token, execute:

$ echo -ne "<USER>:<PASSWORD>" | base64 --wrap 0

Set header with Bearer authentication token:

$ curl -H "Authorization: Bearer <ACCESS_TOKEN>"

Set header with OAuth authentication token:

$ curl -H "Authorization: OAuth <ACCESS_TOKEN>"

Set username and password for Proxy:

$ curl --proxy-user <PROXY_USER>:<PROXY_PASSWORD>

If proxy requires authentication using the NTLM method, add --proxy-ntlm option, if it requires Digest add --proxy-digest.

Add header with API-key:

$ curl -H "<X-API-KEY>:<API_KEY>"

Set Multiple Headers in cURL

Add multiple headers:

$ curl -H "Accept-Charset: utf-8" \
       -H "Content-Type: application/x-www-form-urlencoded" \
       -H "Connection: keep-alive"

3 Replies to “cURL: Add Header, Multiple Headers, Authorization”

  1. Thank you a great guide!

  2. Nice! Thanks bro!

  3. Thank you

Leave a Reply