Jonathan Allard
Jonathan Allard

Reputation: 19269

Getting only response header from HTTP POST using cURL

One can request only the headers using HTTP HEAD, as option -I in curl(1).

$ curl -I /

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?

Upvotes: 764

Views: 925203

Answers (11)

Musty
Musty

Reputation: 1

To get only the response header, use the silent output -s along side -i, then output only the first 10 lines using the head command.

curl -si 0:80 | head

Upvotes: 0

kev
kev

Reputation: 161954

The -w, --write-out <format> option can be very helpful. You can get all http headers, or a single one:

$ curl -s -w '%{header_json}' https://httpbin.org/get -o /dev/null
{"date":["Sun, 18 Feb 2024 13:47:12 GMT"],
"content-type":["application/json"],
"content-length":["254"],
"server":["gunicorn/19.9.0"],
"access-control-allow-origin":["*"],
"access-control-allow-credentials":["true"]
}

$ curl -s -w '%header{content-type}' https://httpbin.org/get -o /dev/null
application/json

read more

Upvotes: 6

kaiser
kaiser

Reputation: 22363

Much easier – this also follows links.

curl -IL http://shortlinktrack.er/in-the-shadows
  • -I is an alias of --head, the man page states that it fetch the headers only
  • -L is an alias of --location, the man page states that curl will follow the location header if there is one

Upvotes: 36

andrew cooke
andrew cooke

Reputation: 46872

-D, --dump-header <file>
       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.

and

-S, --show-error
       When used with -s, --silent, it makes curl show an error message if it fails.

from the man page. so

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.

Upvotes: 954

Shivam Bhardwaj
Shivam Bhardwaj

Reputation: 5

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

Upvotes: 0

zainengineer
zainengineer

Reputation: 13889

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.

Upvotes: 112

Frank N
Frank N

Reputation: 10416

headcurl.cmd (windows version)

curl -sSkv -o NUL %* 2>&1
  • 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

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'

Upvotes: 13

exebook
exebook

Reputation: 33980

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

curl -svo. <URL>

Explanation:

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

Upvotes: 33

Daniel A. R. Werner
Daniel A. R. Werner

Reputation: 866

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

Upvotes: 16

siracusa
siracusa

Reputation: 3670

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.

Upvotes: 300

fiatjaf
fiatjaf

Reputation: 12177

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

or

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

will do the job.

Upvotes: 61

Related Questions