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
Was it useful? Share this post with the world!

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

  1. Thank you a great guide!

  2. Nice! Thanks bro!

  3. Thank you

  4. Hi, My project setup is in such a way that the url’s environment is defined in the karate.config file.. so In my feature file I just define the additional endpoint.
    Eg: in feature file, if env = dev =>
    * def endpoint = https://www.dev-xyz.com/endpoint
    if env = sig =>
    * def endpoint = https://www.stg-xyz.com/endpoint

    Now, how can I add this url to my curl command? I tried the below format but it isn’t working (NOTE: I only added the partial curl to give an idea of how I called my endpoint).. Please help!
    * def result = karate.exec(“curl -X ‘GET’ ‘${endpoint}’ -H ‘accept: application/json’ “)

Leave a Reply