315

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

Noel Yap
20k22 gold badges109 silver badges162 bronze badges
asked Nov 18, 2010 at 7:21
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, 2025 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
Matthias Braun
34.9k27 gold badges158 silver badges177 bronze badges
answered Nov 18, 2010 at 7:27
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
Pat Myron
4,6664 gold badges30 silver badges52 bronze badges
answered Jul 25, 2017 at 21:13

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

answered Sep 5, 2013 at 20:39

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

answered Dec 26, 2022 at 7:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.