1636

I wish to send a header to my Apache server on a Linux box. How can I achieve this via a cURL call?

Syscall
  • 16,959
  • 9
  • 22
  • 41
gagneet
  • 31,111
  • 28
  • 70
  • 99
  • 81
    There is a good way to learn how to use curl for http requests by examples. Download the newest version of Postman, make any http request configuration as you wish at user interface level (post, put, get.. for instance, with headers and json body ) and then click in "generate code" and choose "curl" option. It gives you the equivalent command line. – Vinicius Lima May 28 '16 at 22:51

10 Answers10

2014

man curl:

   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H "Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they will only mess things up for you.

          See also the -A/--user-agent and -e/--referer options.

          This option can be used multiple times to add/replace/remove  multi-
          ple headers.

Example:

curl --header "X-MyHeader: 123" www.google.com

You can see the request that curl sent by adding the -v option.

Daniel Stenberg
  • 44,219
  • 12
  • 115
  • 175
Tader
  • 23,022
  • 5
  • 24
  • 25
  • 79
    If you want to send multiple headers use more than one --header, it is ok, curl will parse each as a different header. There is no way to separate headers inside the same --header parameter. example: curl --header "Accept: javascript" --header "test: hello" -v www.google.com – Hatoru Hansou Sep 24 '15 at 06:41
  • 2
    If people want examples I'll just leave this here: http://bropages.org/ – Peter Westmacott Aug 15 '16 at 10:57
  • 1
    man pages (on OSX, at least) now DO include an example: Example: # curl -H "X-First-Name: Joe" http://192.168.0.1/ – JESii Apr 06 '17 at 13:51
  • 6
    @MartinKonicek and others: I HIGHLY recommend the tldr utiltiy (brew, etc install tldr). Its only examples. eg "- Send a request with an extra header, using a custom HTTP method: curl -H 'X-My-Header: 123' -X PUT http://example.com" –  Dec 13 '17 at 16:41
  • This post should be the accepted answer. The currently accepted answer, however correct, only answers the OPs question implicitly. – Robert Mar 24 '21 at 08:52
734

GET:

with JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

with XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

For posting data:

curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

For logging into a site (auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
Randhi Rupesh
  • 12,692
  • 7
  • 24
  • 45
  • what is meant by @filename for the RESTful post? are you POSTing a file to a REST server? that seems odd to me – JesseBoyd Oct 16 '17 at 18:17
  • 11
    For people arriving later who might be wondering the same thing... @ notation is a way of reading the data to send to the server from a file, rather than inline-ing it into the curl request. You don't POST a file per se, you're POSTing the contents of the file as the body of your POST request. – f1dave Oct 24 '17 at 02:05
  • More detailed answer here: https://stackoverflow.com/questions/14978411/http-post-and-get-using-curl-in-linux/14978657 :) – Amith Koujalgi Dec 19 '17 at 18:09
  • If I understand the usage of headers correctly: header `Accept` is meant for the *client* (asking/requesting), who wishes to have this, BUT the Header `Content-Type` is only the servers *answer* nothing more, it is not mistakenly the wish of the client: *“I want this type of content”* . Right? So for **GET** `curl -i -H "Accept: application/json" http://hostname/resource` should be it. Am I wrong? See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type «In responses, a `Content-Type` header tells the client what the content type of the returned content actually is.» – andreas.naturwiki Jun 22 '20 at 09:46
  • 1
    @andreas.naturwiki, again not to be confused. From MDN, «In responses...» means `Content-Type` at the response. Not the request. `Content-Type` will always refer to type of data transmitted between two parties. If it is on request header, it means the client says 'Yeah I'm sending you data type `application/json`' to the server. If it is on response, it means the server says 'Now I'm sending you data type `text/plain`' to the client. – fa wildchild Dec 24 '20 at 06:06
286

In PHP:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));

or you can set multiple:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));
simhumileco
  • 21,911
  • 14
  • 106
  • 90
James
  • 3,006
  • 1
  • 14
  • 3
  • 1
    @James it works fine in some cases, but in others CURL sends an additional header "Expect: 100-continue" - any idea on how to remove it ? – coding_idiot Feb 12 '13 at 23:50
  • @coding_idiot: You can pass "Expect:" in the array of header values to disable it. Ex.: curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue', 'Expect:')); – ether Sep 09 '13 at 18:12
  • 13
    OP didn't say anything about PHP thought – hanshenrik Dec 03 '15 at 11:26
  • Header name is upper cased with underscores, and HTTP_ is prefixed. For example, "protection-token" becomes "HTTP_PROTECTION_TOKEN". – Bimal Poudel Nov 27 '17 at 19:11
  • @hanshenrik That's true, but OP didn't say anything about the command line either. In fact, the question should have been put on hold for being unclear. Today a newbie wouldn't get away with such a sloppy question. – fluctuating psychosis Aug 01 '20 at 07:50
58

Use -H or --header.

Man page: http://curl.haxx.se/docs/manpage.html#-H

Community
  • 1
  • 1
Greg
  • 295,929
  • 52
  • 357
  • 326
48

GET (multiple parameters):

curl -X  GET "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl --request  GET "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl  "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl -i -H "Application/json" -H "Content-type: application/json"  "http://localhost:3000/action?result1=gh&result2=ghk"
Vietnhi Phuvan
  • 2,274
  • 2
  • 22
  • 25
22

I use Postman.

Execute whatever call you want to do. Then, postman provides a handy tool to show the curl code .

Run it in the terminal. enter image description here

enter image description here

  • 1
    This is a good hack to speed things up but be careful of escaping the single quotes or double quotes if you are using shell script on windows as shell script has it's own formatting requirements – Thierrydev May 15 '19 at 10:56
  • 1
    While postman is nice tool but when you dont have a graphical environment like in Kubernetes pods it is useless. Learn curl and you can always test rest. – Namphibian Feb 27 '20 at 00:18
  • Thank you, this tool turned out to be very useful. – JonathanX64 Nov 24 '20 at 12:34
15

You can also send multiple headers, data (JSON for example), and specify Call method (POST,GET) into a single CUrl call like this:

curl -X POST(Get or whatever) \
  http://your_url.com/api/endpoint \
  -H 'Content-Type: application/json' \
  -H 'header-element1: header-data1' \
  -H 'header-element2: header-data2' \

......more headers................

  -d '{
  "JsonExArray": [
    {
      "json_prop": "1",
    },
    {
      "json_prop": "2",
    }
  ]
}'
LeMeme
  • 276
  • 4
  • 7
10

I've switched from curl to Httpie; the syntax looks like:

http http://myurl HeaderName:value
Graham Perks
  • 21,623
  • 8
  • 56
  • 79
7

In case you want send your custom headers, you can do it this way:

curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk
simhumileco
  • 21,911
  • 14
  • 106
  • 90
Palsri
  • 422
  • 3
  • 13
3

In anaconda envirement through windows the commands should be: GET, for ex:

curl.exe http://127.0.0.1:5000/books 

Post or Patch the data for ex:

curl.exe http://127.0.0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\"rating\":\"2\"}' 

PS: Add backslash for json data to avoid this type of error => Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

and use curl.exe instead of curl only to avoid this problem:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type
"System.String" to type "System.Collections.IDictionary".
At line:1 char:48
+ ... 0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\" ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
DINA TAKLIT
  • 4,946
  • 7
  • 42
  • 50
  • This has almost nothing to do with the original posted question. – MarkHu Feb 23 '21 at 17:18
  • @MarkHu It responses to the question headline, not the question body :). The is many questions similar to the question headline so they will take adv of my answer and I was one of them so once I find the answer I shared it. – DINA TAKLIT Feb 23 '21 at 20:21