Curl linux get headers

Getting only response header from HTTP POST using cURL

Lengthy HTML response bodies are a pain to get in command-line, so I’d like to get only the header as feedback for my POST requests. However, HEAD and POST are two different methods. How do I get cURL to display only response headers to a POST request?

9 Answers 9

-D, --dump-header Write the protocol headers to the specified file. This option is handy to use when you want to store the headers that a HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is however a better way to store cookies. 
-S, --show-error When used with -s, --silent, it makes curl show an error message if it fails. 
curl -sS -D - www.acooke.org -o /dev/null 

follows redirects, dumps the headers to stdout and sends the data to /dev/null (that’s a GET, not a POST, but you can do the same thing with a POST — just add whatever option you’re already using for POSTing data)

note the — after the -D which indicates that the output «file» is stdout.

above comment is valid if you’re using powershell. for cmd.exe use curl -s -D — http://yahoo.com -o nul

@mamachanko -D takes an argument that says where the output should go. the single dash means it should go to stdout.

The other answers require the response body to be downloaded. But there’s a way to make a POST request that will only fetch the header:

curl -s -I -X POST http://www.google.com 

An -I by itself performs a HEAD request which can be overridden by -X POST to perform a POST (or any other) request and still only get the header data.

This answer is actually correct because web servers can return different headers based on request method. If you want to check headers on GET, you have to use GET request.

This is the most correct answer, in my opinion. It is easy to remember, it actually sends GET request and doesn’t download the whole response body (or at least doesn’t output it). The -s flag is nor necessary.

@JeffPuckettII well kinda nitpicking I would say. You can replace GET with POST in above command and it will work as expected. or any other is key there.

This does not work when you actually want to POST some data. Curl says: Warning: You can only select one HTTP request method! You asked for both POST Warning: (-d, —data) and HEAD (-I, —head).

@nickboldt The point here is that a server might respond differently to a HEAD request than to a POST or GET request (and some servers actually do that), so -X HEAD is no reliable solution here.

Читайте также:  Linux systemctl create service

The Following command displays extra informations

curl -X POST http://httpbin.org/post -v > /dev/null 

You can ask server to send just HEAD, instead of full response

curl -X HEAD -I http://httpbin.org/ 

Note: In some cases, server may send different headers for POST and HEAD. But in almost all cases headers are same.

It’s unfortunate that the other answer won, because this is the correct answer — it doesn’t unnecessarily transfer a ton of data.

@dmd If I understand the cURL manual for -X, —request correctly, -X HEAD still results in “a ton of data” but there is -I, —head which should results in what you are anticipating.

Problem with -X HEAD is that the server might respond differently, since it now receives a HEAD request instead of a GET (or whatever the previous request was)

Warning: Setting custom HTTP method to HEAD with -X/—request may not work the Warning: way you want. Consider using -I/—head instead.

For long response bodies (and various other similar situations), the solution I use is always to pipe to less , so

curl -i https://api.github.com/users | less 
curl -s -D - https://api.github.com/users | less 

these are not equivalent. the first issues a HEAD request to which many servers respond differently. the second issues a GET request which is more like what we are looking for here.

Maybe it is little bit of an extreme, but I am using this super short version:

-v print debug information (which does include headers)

-o. send web page data (which we want to ignore) to a certain file, . in this case, which is a directory and is an invalid destination and makes the output to be ignored.

-s no progress bar, no error information (otherwise you would see Warning: Failed to create the file .: Is a directory )

warning: result always fails (in terms of error code, if reachable or not). Do not use in, say, conditional statements in shell scripting.

@bfontaine there are other answers that show how to do this the most correct way, this one is here to show the short alternative that does the same thing basically.

You should clarify in your answer that this command always fails. curl -svo. && echo foo won’t print foo because -o. make curl return a non-zero (= error) code: curl: (23) Failed writing body .

a «solution» that ends with returning an error is not a valid solution. it’s a happy accident. if something goes wrong, you have no way of knowing because you’ve already swallowed the error

Much easier – this also follows links.

curl -IL http://example.com/in-the-shadows 

While the other answers have not worked for me in all situations, the best solution I could find (working with POST as well), taken from here:

curl -vs ‘https://some-site.com’ 1> /dev/null

Whether this is necessary or not might depend on url and used shell. I improved the answer accordingly. Thanks.

headcurl.cmd (windows version)

  • I don’t want a progress bar -s ,
  • but I do want errors -S ,
  • not bothering about valid https certificates -k ,
  • getting high verbosity -v (this is about troubleshooting, is it?),
  • no output (in a clean way).
  • oh, and I want to forward stderr to stdout, so I can grep against the whole thing (since most or all output comes in stderr)
  • %* means [pass on all parameters to this script] (well(https://stackoverflow.com/a/980372/444255), well usually that’s just one parameter: the url you are testing
Читайте также:  Vpn server on linux mint

real-world example (on troubleshooting proxy issues):

C:\depot>headcurl google.ch | grep -i -e http -e cache 
Hostname was NOT found in DNS cache GET HTTP://google.ch/ HTTP/1.1 HTTP/1.1 301 Moved Permanently Location: http://www.google.ch/ Cache-Control: public, max-age=2592000 X-Cache: HIT from company.somewhere.ch X-Cache-Lookup: HIT from company.somewhere.ch:1234 

Linux version

for your .bash_aliases / .bash_rc :

alias headcurl='curl -sSkv -o /dev/null $@ 2>&1' 

Источник

cURL – How to display request headers and response headers

Request Header and Response Header are both a part of the HTTP protocol, which is the standard used for communication between web browsers and web servers. The Request Header is sent by the browser as part of an HTTP request, and it contains information such as the type of request, the URL of the requested page, and any authentication credentials. The Response Header is sent by the server in response to the request, and it contains information such as the status code of the response, the content type of the page, and any authentication credentials.

Together, the Request and Response Headers help to ensure that data is sent securely and accurately between the browser and the server. Request and Response Headers are essential for web developers as they provide important information for debugging and troubleshooting. If you’re interested in learning more about Request and Response Headers, a good place to start is by reading up on the HTTP protocol.

cURL is a command line utility used to transmit data over different-2 protocols. It is a quick tool for developers to view the request header and response header values of a website.

1. cURL – Get Request Headers

Use —versbose or -v option with the curl command to fetch the request header and response header values as following:

how to get request and response header with curl

2. cURL – Get Response Headers

You can also use curl to fetch the response header values only. Use -I option to get the response header values.

Output:
HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Sat, 10 Sep 2022 09:25:56 GMT Expires: Mon, 10 Oct 2022 09:25:56 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN

3. cURL – Get Custom Header Values

Sometimes you may need to fetch the specific header value. That is helpful for scripting and many other tasks. Use the grep command to filter specific values from complete header values. The -F is used to search fixed string and -i is used for case-sensitive search.

curl -I google.com | grep -Fi "Content-Type" 
Output:
Content-Type: text/html; charset=UTF-8

Wrap Up

cURL is a command line utility that is helpful for multiple tasks. We can also use curl to request a server for the details. This tutorial helped you to get the request header and response header values using the curl command line.

Источник

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?

Читайте также:  Загрузить файл через терминал linux

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.

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 

Источник

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