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: 192.168.0.1" http://example.com

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

$ curl -H "Accept: application/xml" -X GET www.example.com

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"}' \
       http://example.com

POST data from data.json file:

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

Example of the data.json file content:

$ cat data.json
{
  "key1":"value1",
  "key2":"value2"
}

Set Authorization Header in cURL

Basic authentication using Username and Password:

$ curl --user <USER>:<PASSWORD> http://www.example.com

Set header with Basic authentication token:

$ curl -H "Authorization: Basic <ACCESS_TOKEN>" http://www.example.com

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>" http://www.example.com

Set header with OAuth authentication token:

$ curl -H "Authorization: OAuth <ACCESS_TOKEN>" http://www.example.com

Set username and password for Proxy:

$ curl --proxy-user <PROXY_USER>:<PROXY_PASSWORD> http://www.example.com

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>" http://www.example.com

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"
       http://example.com

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