The Basics of HTTP - Part 3 - The Response

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 Messages

As 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
Headers Date: Mon, 13 March 2009 22:38:34 GMT
Server: Apache/2.2 (Win32)
Last-Modified: Wed, 12 Mar 2009 23:11:55 GMT
Accept-Ranges: bytes
Content-Length: 78
Connection: close
Content-Type: text/html; charset=UTF-8

Body
<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:

  1. OK
  2. Moved Permanently
  3. Found
  4. Not Modified
  5. Bad request
  6. Unauthorized
  7. Forbidden
  8. Not Found
  9. Method Not Allowed
  10. I'm a Teapot (Thanks to Peter Farrell link)
  11. Internal Server Error
  12. Not Implemented
  13. Bad Gateway
  14. Service Unavailable
  15. 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.

Conclusion

There is A LOT more to know about HTTP. I have only just scratched the surface. I may blog more about it in the future, but right now I have other things I need to work on and about which I would like to write.

Comments
Rick O's Gravatar Do you think you'll go into detail on the subtle differences between the status codes? Such as 301 vs 302, and 200 vs 204 vs 206? They may be a bit more detailed than you were trying to go, but they also illustrate the versatility of HTTP and why it is used for so many different things.

I'm digging this series, though. I'm trying to nitpick, but so far you are doing a commendable job!
# Posted By Rick O | 3/13/09 5:32 PM
Jason Dean's Gravatar @Rick, Thanks. If I ever decide to continue this, I think that would be a great idea. Maybe this summer if I run out of things to write about.
# Posted By Jason Dean | 3/16/09 9:32 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1. Contact Blog Owner