• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Show the headers only for a request with cURL

Show the headers only for a request with cURL

cURL is an extremely useful command line tool for making HTTP requests and can be used for diagnosing errors, downloading content and so. But what if you just want to see the response headers to see, for example, if a page is doing the right sort of redirect?

tl;dr

To do a HEAD request and show the headers:

curl -I [url]

To do a GET request and show the headers only:

curl -sD - -o /dev/null https://www.electrictoolbox.com/

Using the -v verbose flag:

curl -s -v -o /dev/null https://www.electrictoolbox.com/

Using the -I or –head arguments to do a HEAD request

The simplest solution is to use -I (capital i) or –head to do a HEAD request like so:

$ curl -I https://www.electrictoolbox.com/
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:31:49 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
$ curl -I https://electrictoolbox.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:31:53 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://www.electrictoolbox.com/

The first example is for a page that returns content with a 200 HTTP response code, and the second a 301 redirect and the location to redirect to.

How to get the headers for a regular GET request

Th -I flag is useful, but sometimes web pages return a different response depending if you make a regular GET request compared with a HEAD request.

Calling curl with the URL on its own will dump the HTML to the command line (curl https://www.electrictoolbox.com/) and calling it with the -o flag allows you to dump the HTML to a file instead (curl -o /tmp/index.html https://www.electrictoolbox.com/).

Using the -D or –dump-header will dump the headers to a file, but you can pass – as the filename to dump them to the comment line. You can then use -o /dev/null to not show the HTML, or alternatively an actual filename to save the HTML to if you want.

And finally, when dumping the HTML to a file, curl shows a progress bar, which can be supressed with the -s flag.

Putting this all together, this command will show the headers only: 

curl -s -D - -o /dev/null https://www.electrictoolbox.com/

And you can combine the -s & -D flags together like this:

curl -sD - -o /dev/null https://www.electrictoolbox.com/

After all, here’s the response:

HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Sun, 23 Aug 2015 21:43:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding

Using the verbose flag instead

And a final option is to substitute the -v flag for -D, which will show both the request headers and response headers and show any errors etc:

$ curl -s -v -o /dev/null https://www.electrictoolbox.com/
*   Trying 173.230.149.142...
* Connected to www.electrictoolbox.com (173.230.149.142) port 80 (#0)
> GET / HTTP/1.1
> Host: www.electrictoolbox.com
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.6.3
< Date: Sun, 23 Aug 2015 21:44:45 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< 
{ [2688 bytes data]
* Connection #0 to host www.electrictoolbox.com left intact

Check Out These Related posts:

  1. Get a MySQL table structure from the INFORMATION_SCHEMA
  2. Getting HTTP headers with Lynx
  3. PHP’s file_get_contents and CURL functions
  4. How to use a flash card reader with Linux – How to scan the SCSI bus

Filed Under: Linux/Unix/BSD

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap