Response Code #FFF

So, you have mastered semantic HTML, standards CSS and unobtrusive JavaScript… What else is there to learn in our attempts to become masters of the intertubes? Heard of HTTP response codes? And do you know how to use them properly? No? Read on…

HyperText Transfer Protocol, or HTTP as we like to call it, is a transport protocol that safely delivers HTML from your server to your clients’ browsers, but for too long man with mouse has been neglecting the very soul of the protocol: Response codes. Without these three digit codes, your browser isn’t able to work out is a request was successful, whether it failed or whether it needs to look elsewhere.

This isn’t just useful for your browser either — search engines (such as Google, which you might have heard of) use these response codes to make decisions about whether to keep things in their cache or not, so using them correctly can be really important to your SEO plan. (You’ll have to hit up one of the resident SEO experts to find out what exactly happens — buy one a beer, they aren’t all evil…)

So if these codes are so damn important, why haven’t you heard of them before? Well, you have, you just don’t know it — 404 Not Found is probably the most (in)famous of all the HTTP codes, only because it is one of the only ones that is directly visible to the user. If a server returns a 404 Not Found it means that either the server misplaced the file, or more likely it just can’t find it. Unsuprisingly, every request you make to a server will return a response code, grouped into five groups, denoted by the first number:

1xx: Information
2xx: Success
3xx: Redirection
4xx: Client Error
5xx: Server Error

Whilst I could list them all, I’m lazy and you have a short attention span, so here is a list of the more interesting ones: (if YouTube and Twitter haven’t yet ruined your concentration yet, check out RFC2616 for the full list.)

200 - OK This is probably the most common response code - it means the request was successful
201 - Created If the last request created something on the server, we can return this.

301 - Moved permanently We can use this to notify the caller that the action they performed has moved the item elsewhere, and that all it’s references to the resource should be updated.
302 - Found This actually means Moved temporarily. Browsers that get this should redirect the user
to a new URL, but at some point the URL will be back, so they can still use this resource in the future.

400 - Bad Request The requested GET wasn’t valid. Useful when requiring search query paramaters to filter results for example
403 - Forbidden Tell the user they aren’t allowed to access the requested resource
404 - Not Found The requested information wasn’t found on the server.
405 - Not allowed Says that the request that was made wasn’t allowed. If you have a resource that is read only for example, you may throw this if the user tries to POST, PUT or DELETE the resource
410 - Gone Useful if the resource that has been requested have been permanently deleted, and will never be restored. Ever.
422 - Invalid Entity Technically a WebDAV extension, and not part of HTTP/1.1, but this is really handy for telling the client that they haven’t sent enough information

500 - Server Error Something bad happened. Basically the HTTP equivalent to an Exception
503 - Server unavailable - the Twitter (Fail Whale) code. Use this if you are upgrading your server and aren’t able to take more requests

Now, I know you are champing at the bit to try out some of these funky new codes, and you can try them out today for the low, low introductory price of free, just by sending the right response using your favourite language’s built in functions. Status codes need to be sent in the header of a response, so you might need to read up on how to do that in your lanugage or choice, but here are a couple of examples:

Ruby on Rails:
# This will return a 201 Created status code
render :status => 201

PHP:
// This will return a 410 Gone status code
header(”HTTP/1.1 410 Gone”);

ASP.NET:
// This will redirect to user to www.webindustry.asn.au permanently
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”, “http://www.webindustry.asn.au”);
Response.AddHeader(”301 Moved Permanently”, “http://www.webindustry.asn.au”);

So, be a good netizen and use response codes properly — your Googles will thank you.