299

How would I pass multiple values in the header for a curl request?

3
  • Do you have an example you're using right now that you could show us?
    – Ryan Bigg
    Nov 18, 2010 at 7:23
  • 12
    add multiple -H flag. For example curl -H "Content-Type : application/json" -H "Authorization : Token token='yourtokenhere'"
    – gsumk
    Jul 18, 2019 at 18:37
  • 1
    How do you feel about accepting an answer below? I like mine heh ;) @Noel Yap Feb 16, 2021 at 7:40

4 Answers 4

375

Just use the -H option several times:

curl -H "Accept-Charset: utf-8" -H "Content-Type: application/x-www-form-urlencoded" http://www.some-domain.example
2
  • 4
    What if the header contains "?
    – Freewind
    Apr 8, 2011 at 8:45
  • 6
    @Freewind wrap the value with a single-quote instead of a double, or escape it. Same thing you always do in that case. Jun 26, 2015 at 19:15
85

To pass multiple headers in a curl request you simply add additional -H or --header to your curl command.

Example

//Simplified
$ curl -v -H 'header1:val' -H 'header2:val' URL

//Explanatory
$ curl -v -H 'Connection: keep-alive' -H 'Content-Type: application/json'  https://www.example.com

Going Further

For standard HTTP header fields such as User-Agent, Cookie, Host, there is actually another way to setting them. The curl command offers designated options for setting these header fields:

  • -A (or --user-agent): set "User-Agent" field.
  • -b (or --cookie): set "Cookie" field.
  • -e (or --referer): set "Referer" field.
  • -H (or --header): set "Header" field

For example, the following two commands are equivalent. Both of them change "User-Agent" string in the HTTP header.

    $ curl -v -H "Content-Type: application/json" -H "User-Agent: UserAgentString" https://www.example.com
    $ curl -v -H "Content-Type: application/json" -A "UserAgentString" https://www.example.com
62

Sometimes changing the header is not enough, some sites check the referer as well:

curl -v \
     -H 'Host: restapi.some-site.com' \
     -H 'Connection: keep-alive' \
     -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
     -H 'Accept-Language: en-GB,en-US;q=0.8,en;q=0.6' \
     -e localhost \
     -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36' \
     'http://restapi.some-site.com/getsomething?argument=value&argument2=value'

In this example the referer (-e or --referer in curl) is 'localhost'.

0
2

Set Header -H "Content-Type: application/json" using GET Request

Example -

curl -H "Content-Type: application/json" -X GET https://jsonplaceholder.typicode.com/posts/1

If u want to Add multiple header

curl -H "Connection: keep-alive" -H "Content-Type: application/json -X GET https://jsonplaceholder.typicode.com/posts/1

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.