Linux curl http headers

How to display request headers with command line curl

Command line curl can display response header by using -D option, but I want to see what request header it is sending. How can I do that?

Note on using —head / -I : not all servers respond exactly the same to HEAD requests (for example, Content-Encoding would be missing if you were attempting to verify that the body would be gzipped) and not all servers support HEAD . -v is usually the safer choice.

I didn’t get it since any modification in order to fulfill OP’s request (pun wasn’t intended) requires changes on the command (i.e. curl and its arguments) and since command sets the request headers you already see those. AFAIK the request is all in or nothing, so either all the headers will be sent, or none. Am I missing something?

9 Answers 9

curl’s -v or —verbose option shows the HTTP request headers, among other things. Here is some sample output:

$ curl -v http://google.com/ * About to connect() to google.com port 80 (#0) * Trying 66.102.7.104. connected * Connected to google.com (66.102.7.104) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3 > Host: google.com > Accept: */* > < HTTP/1.1 301 Moved Permanently < Location: http://www.google.com/ < Content-Type: text/html; charset=UTF-8 < Date: Thu, 15 Jul 2010 06:06:52 GMT < Expires: Sat, 14 Aug 2010 06:06:52 GMT < Cache-Control: public, max-age=2592000 < Server: gws < Content-Length: 219 < X-XSS-Protection: 1; mode=block <  301 Moved 

301 Moved

The document has moved here. * Connection #0 to host google.com left intact * Closing connection #0

@jacobsimeon I thinks that’s because it shows not only the Request headers but also the Response headers and Response body.

Читайте также:  Средство удаленного администрирования linux

A popular answer for displaying response headers, but OP asked about request headers.

curl -s -D - -o /dev/null http://example.com 
  • -s : Avoid showing progress bar
  • -D — : Dump headers to a file, but — sends it to stdout
  • -o /dev/null : Ignore response body

This is better than -I as it doesn’t send a HEAD request, which can produce different results.

It’s better than -v because you don’t need so many hacks to un-verbose it.

Even though this question asks for request headers, google is directing everybody here who is looking for response headers so we are all glad this answer is here. And this answer is the best for getting response headers. Thanks.

I believe the command line switch you are looking for to pass to curl is -I .

$ curl -I http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287 HTTP/1.1 301 Moved Permanently Date: Sat, 29 Dec 2012 15:22:05 GMT Server: Apache Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/ Content-Type: text/html; charset=iso-8859-1 

Additionally, if you encounter a response HTTP status code of 301, you might like to also pass a -L argument switch to tell curl to follow URL redirects, and, in this case, print the headers of all pages (including the URL redirects), illustrated below:

$ curl -I -L http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287 HTTP/1.1 301 Moved Permanently Date: Sat, 29 Dec 2012 15:22:13 GMT Server: Apache Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/ Content-Type: text/html; charset=iso-8859-1 HTTP/1.1 302 Found Date: Sat, 29 Dec 2012 15:22:13 GMT Server: Apache Set-Cookie: UID=b8c37e33defde51cf91e1e03e51657da Location: noaccess.php Content-Type: text/html HTTP/1.1 200 OK Date: Sat, 29 Dec 2012 15:22:13 GMT Server: Apache Content-Type: text/html 

Источник

How to send a header using a HTTP request through a cURL call?

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.

Читайте также:  Прошивка iphone на linux

11 Answers 11

 -H/--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 1: Single Header

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

Example 2: Multiple Headers

curl --header "Accept: text/javascript" --header "X-Test: hello" -v www.google.com 

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

Источник

Оцените статью
Adblock
detector