Get a Single Header with cURL

By  on  

Debugging third party apps can be difficult for a variety of reasons.  You take for granted that the third party has not only properly coded their app but that their server is also serving files properly.  As more a developer than a sysadmin I tend to spend a long time on the code before I ensure the server stuff is correct.  One mistake often made on the server side is not sending correct Content-Type headers with content, especially audio and video files, and that can cause a real problem for the app or codec trying to do something with the content.  These days I check response headers before I do anything else.

Getting complete response headers with cURL is easy:

#  Get response headers
curl -I https://davidwalsh.name

#  Result:
#  HTTP/1.1 301 Moved Permanently
#  Date: Wed, 03 Feb 2016 17:29:51 GMT
#  Content-Type: text/html; charset=iso-8859-1
#  Connection: keep-alive
#  Set-Cookie: __cfduid=dab1e532a38b35ce7d764344217ddd8dc1454520590; expires=Thu, 02-Feb-17 17:29:50 GMT; path=/; domain=.davidwalsh.name; HttpOnly
#  Location: https://davidwalsh.name/
#  Cache-Control: max-age=1
#  Expires: Wed, 03 Feb 2016 17:29:52 GMT
#  Vary: Accept-Encoding
#  Server: cloudflare-nginx
#  CF-RAY: 26efdc3d2aa841ef-MSP

But that's a lot of info to sift through if you only want the one header.  Here's how you can retrieve just one header:

#  Get single header only
#  curl {url} -I | grep -Fi {header_name}
curl https://davidwalsh.name -I | grep -Fi Content-Type

#  Result:
#  Content-Type: text/html; charset=iso-8859-1

With the extra grep above you'll only get the one header back.  Simple!

Recent Features

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

  • By
    Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

  • By
    iPhone-Style Passwords Using MooTools PassShark

    Every once in a while I come across a plugin that blows me out of the water and the most recent culprit is PassShark: a MooTools plugin that duplicates the iPhone's method of showing/hiding the last character in a password field. This gem of...

Discussion

  1. Lev Nar

    this is the best answer that I found with my problem, but the only question i have is how to do this is curl php

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!