All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

Curl for HTTP Debugging

25 August 2004

When developing a web application, it’s essential to be able to debug any problems at as low a level as possible. When experiencing problems with a form handler, for example, it’s common practise to echo out all the data in the POST to see if anything looks odd – if you’re not getting the data you’re expecting from the form, then there’s no point debugging all your code.

When dealing with issues involving cookies, character sets, error codes, other HTTP headers and such, this can often be difficult to debug with a standard browser. Most browsers go to great lengths to hide away the nasties of HTTP from the user, and as a developer this can make debugging difficult. You basically end up tracking a circumstance by a browser’s reaction to a circumstance, which is often far from ideal.

Whilst some browsers (primarily Firefox) have extensions available for displaying HTTP headers and the like, let us not forget the speed and convenience of the unix command-line tool curl. A quick call to curl -I http://allinthehead.com/ results in:

HTTP/1.1 200 OK
Date: Wed, 25 Aug 2004 15:37:30 GMT
Server: Apache
X-Powered-By: One fiercely courageous armadillo invoking, The blood, sweat and tears of the fine, fine TextDrive staff
Vary: Accept-Encoding
Served-By: TextDrive
Connection: close
Content-Type: text/html; charset=utf-8

Which quickly reveals the HTTP error code (200, all’s good), that my character set is set to UTF-8 as I’d intended, and so on. Drop the -I argument, and you’d get the full source of the page returned. Try curl --user-agent 'GoogleBot' http://allinthehead.com/ and see how the GoogleBot sees my site (the same as everyone else, but hey).

This is nothing revolutionary, of course, but it’s something I find useful on a regular basis, so I thought I’d share. If you run OS X you’d got curl right there from the terminal, and it seems to be pretty common on linux and unix machines generally. I use curl from Debian pretty frequently too.

- Drew McLellan

Comments

  1. § Noah: A long time ago I wrote a PHP script that you can pass a URL to and it will output the full request headers, reponce headers and source of the request resource.

    If anyone is interested in having a copy feel free to email me.
  2. § since1968: How did you replace PHP/4.3.8 with a courageous armadillo?
  3. § Drew McLellan: I just set the X-Powered-By header in PHP.

    header('X-Powered-By: Eight pints of strong beer');

    The TXD bit is tagged on the end by Jason – hence the unnecessary comma seen above. I couldn’t think of anything witty that used the comma ;)
  4. § Gabriel Mihalache: I simple telnet on port 80 and a 1-2 HTTP headers types by hand will also do:

    GET / HTTP/1.0
    USER-AGENT: gabzilla

    (2 CR LFs please) and there you go. I’m currently looking for curl for Windows, so I can stop typing in telnet.
  5. § justin: If you’ve got the typical Perl modules installed you can also just use
    GET http://www.whatever.com from the command line. Few characters less to type than curl :)
    Want to see the headers? Just use HEAD http://www.whatever.com

    You can change the X-Powered By: to anything you want via editing the HEX file directly which contains this text. Can’t remember which file it was, but my friend helped with this just yesterday. Much better than adding a header(...) call to every PHP page.
  6. § David House: I think rather than calling them ‘HTTP Error Codes’, ‘HTTP Status Codes’ would be more accurate, as many of them aren’t actually reporting errors ;)

    Nitpicking, yes.
  7. § Anne: It also shows that you are using the incorrect MIME type for XHTML ;-)
  8. § Justin: You might want to give the firefox “web developer” extension a try. It comes with a new tool bar that allows you to dump all header, sessions, cookie, css, etc. into a new browser tab. It’s awesome for developing web applications.
  9. § Ben: cURL is available on all manner of platforms / distros:
    http://curl.haxx.se/
  10. § James: On some unix-ish platforms (well, I’ve used it on Linux and Solaris but I’m sure it exists on others) you can also use ‘wget’.
    To achieve the same as:
    curl -I

    You need to do something like:
    wget -S -O – > /dev/null

    (the -S shows the http headers, the rest is just to stop it outputting or saving the actual html anywhere)

    Obviously curl is easier in this case but I thought I’d mention wget incase anyone ever finds themselves on a box without curl :)
  11. § Giles: I’ve found Wannabrowser to be handy for debugging stuff. Pretty handy for being nosey about other peoples sites too!
  12. § JP Hastings-Spital:

    Noah – and anyone else – I’d love to get my hands on that php script that outputs all the HTTP request headers. You didn’t leave your address here so I hope this gets through – get me here.

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Follow me

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

I made

Perch - a really little cms

About Drew McLellan

Photo of Drew McLellan

Drew McLellan (@drewm) has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.