315

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? Commented Nov 18, 2010 at 7:23
  • 14
    add multiple -H flag. For example curl -H "Content-Type : application/json" -H "Authorization : Token token='yourtokenhere'" Commented Jul 18, 2019 at 18:37
  • similar: Send a HTTP request header through a cURL call? Commented Aug 4 at 22:19

4 Answers 4

396

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
Sign up to request clarification or add additional context in comments.

2 Comments

What if the header contains "?
@Freewind wrap the value with a single-quote instead of a double, or escape it. Same thing you always do in that case.
86

Add additional -H or --header to your curl command:

//Simple
$ 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

For standard HTTP header fields such as User-Agent, Cookie, Host, another way to set them is designated curl command options:

  • -A / --user-agent: "User-Agent"
  • -b / --cookie: "Cookie"
  • -e / --referer: "Referer"
  • -H / --header: "Header"

These commands are equivalent. Both change "User-Agent" 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

Comments

64

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'.

Comments

3

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.