HTTP (HyperText Transfer Protocol) defines how resources get transfered across the web. In the previous article on URLs, I explained that there are other protocols but HTTP is the most important when it comes to RESTful interfaces.
HTTP Methods
When a client (like your web browser) asks for a resource from a web server, it uses one of:
- GET
- PUT
- POST
- DELETE
- HEAD
HTTP GET
This method means “I’d like the resource, please” The client or the server can override the request and grab a copy from a cache.
I’m going to say that again the client can override the request and grab a copy from its cache. That means no network activity, the server doesn’t even know that the resource was requested.
HTTP PUT & POST
Both these can mean “here is a resource for you” They could be an update to an existing resource or create a new one, the HTTP standard does not specify which. We will see later on that in RESTful interfaces a PUT usually means update, whilst POST means create.
POST is commonly used in web forms, so when you buy an item from Amazon or bid on an Ebay auction, you are sending a POST request. They should never be cached.
HTTP DELETE
I bet you can guess what this does!
HTTP HEAD
This is a more interesting method, it means “tell me about the resource, but don’t bother sending the resource” It can be used to check whether a resource exists (by checking the response code), the type of the resource or how old it is (by check the headers).
HTTP Response Codes
The server will answer a HTTP request with a code that indicates whether the request could be processed. I’m sure you know some of these code already, like the dreaded 404!
There’s quite a bunch of them, so here are a select few that are applicable to RESTful APIs:
- 200 OK
- 201 Created
- 202 Accepted
- 304 Not Modified
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Request Entity Too Large
- 414 Request-URI Too Large
- 415 Unsupported Media Type
- 500 Internal Server Error
- 501 Not Implemented
- 503 Service Unavailable
I’ll explain how these might apply to a web service in a later article, when we cover REST in more detail.
HTTP Headers
Both requests and responses can have extra headers attached. If you’ve done any web development you’re bound to know a few common headers (like UserAgent and Referer) already but there are many more that are useful when we’re dealing with APIs.
Request Headers
A classic use of a request header comes about when a resource can be represented in many forms, such as a product description available as a HTML page, XML document, JSON or a photo. In this case, a client might send its GET request with an Accept header indicating that it wants JSON, like this:
Accept: text/json
Another useful request header can be used to get resources that have been updated recently:
If-Modified-Since: Sat, 25 Oct 2008 19:43:31 GMT
Response Headers
Response headers contain meta-data about the requested resource. They can explain what format the data is (Content-Type), how long the data is valid for (the Expires header) or when it was last written (Last-Updated).
Don’t forget that you can use the HEADERS method to get just the response code and headers back from the server.
End Of Part Two
Hopefully you now have a decent grasp on the magic protocol behind the web. I find it really helps if you can think in terms of objects or resources instead of pages, then you’ll get a better feel for exactly what your browser is doing when you ask for a URL.
As always, feel free to ask questions via the comments, I’ll do my best to answer.
This post is part of a series on REST so if you’ve found it useful, subscribe to catch the other articles in the series.




web-development
del.icio.us
Furl
Google Bookmarks
ma.gnolia
reddit
Simpy
Sphinn
StumbleUpon
Yahoo MyWeb
Post On Fire

