So I wasn't sure if I was going to continue this series. I had been sick for a while and I am already done with my presentation (the reason for my research). But I've decided to finish what I started. I will probably finish with this post. If you want to learn more about HTTP, I strongly recommend the O'Reilly HTTP Definitive Guide. It is from 2002, but HTTP has not changed much, if at all, since then, so it is still a very relevant text.
HTTP Response MessagesAs we discussed last time, HTTP requests are made from a client to a server to request a specific resource. The server processes that request and returns a response. That response must also be formatted properly and contain specific information for the client to understand it and either display the resource or display the reason that the resource was unavailable.
Response messages, just like request messages, have 3 parts. A start line, headers, and a body.
|Start Line||HTTP/1.1 200 OK|
Date: Mon, 13 March 2009 22:38:34 GMT
Server: Apache/2.2 (Win32)
Last-Modified: Wed, 12 Mar 2009 23:11:55 GMT
Content-Type: text/html; charset=UTF-8
<html><head><title>My Page</title></head><body>My Content</body></html>
The start line of the response, just like the request, indicates the version of HTTP being used. It also provides a status code to let the browser know what has happened. There are MANY status codes, though most of us will not see, or use, most of them.
Status codes are divided into five groups.
- 1xx - Informational
- 2xx - Success
- 3xx - Redirection
- 4xx - Client Error
- 5xx - Server Error
Some common status codes you will see are:
- Moved Permanently
- Not Modified
- Bad request
- Not Found
- Method Not Allowed
- I'm a Teapot (Thanks to Peter Farrell link)
- Internal Server Error
- Not Implemented
- Bad Gateway
- Service Unavailable
- Gateway Timeout
Status codes are used to let the client know what has happened on the server. The most common (and most enjoyable) status code is 200. 200 means that the request was successful and the resource has been provided. Most other status code indicated that for some reason, the requested resource could not be provided. For example, status code 302 indicates that the resource has been temporarily moved and gives the browser the new location of the resource. The browser then automatically redirects to that location and retrieves the resource (302 is the status code used in a <cflocation> tag).
I strongly encourage you to take a look at the other status codes and learn a little more about them. There is no reason to memorize them, but they are good to know for troubleshooting things like web services and Ajax.
After the start line you will see the response headers. Some of them may be the same header type as those used in request headers, but many are response specific headers. Headers provide additional information about the response that the client may need to know to properly display it. For example, the response header: "Content-Type: text/html; charset=UTF-8" is used by the browser to know the character set that was returned.
Finally, the response header includes a body. For a 200 OK, the body contains the requested resource. Other response types may contain various additional information.
Note: Notice the extra line of white space between the headers and the body. This is a Carriage-Return and a Line Feed (CRLF). This is how the response header separates the headers from the body.