curl usage to get header

LinuxCurl

Linux Problem Overview


Why does this not work:

curl -X HEAD http://www.google.com

But these both work just fine:

curl -I http://www.google.com

curl -X GET http://www.google.com

Linux Solutions


Solution 1 - Linux

You need to add the -i flag to the first command, to include the HTTP header in the output. This is required to print headers.

curl -X HEAD -i http://www.google.com

More here: https://serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head

Solution 2 - Linux

curl --head https://www.example.net

I was pointed to this by curl itself; when I issued the command with -X HEAD, it printed:

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

Solution 3 - Linux

google.com is not responding to HTTP HEAD requests, which is why you are seeing a hang for the first command.

It does respond to GET requests, which is why the third command works.

As for the second, curl just prints the headers from a standard request.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkur AgarwalView Question on Stackoverflow
Solution 1 - Linuxdmc7zView Answer on Stackoverflow
Solution 2 - LinuxbovenderView Answer on Stackoverflow
Solution 3 - LinuxSheetJSView Answer on Stackoverflow