Ajax Bestiary: A Javascript Field Guide
 
Ajax Bestiary: A Javascript Field Guide
 
 

Under The Hood With HTTP: Part 1 Anatomy of A Request

Posted by Don Albrecht

As AJAX & Javascript developers we use HTTP daily, but we sometimes forget the actual data that flows down the wire. Our frameworks, toolkits and browsers have abstracted it all away. Although a knowledge of http is not as important as it used to be, it still comes in handy things go pear shaped and you need to debug. Here is the first in a collection of short introductions to HTTP.

So any HTTP request follows a relatively simple format:

  • A first line
  • An optional collection of header lines
  • CRLF ( An Empty Line);
  • Message body

All initial lines and headers need to end in CRLF although the specification calls for lines ending LF be gracefully handled. (CR is ASCII Value 13 LF is ASCII Value 10 );

The Initial Request Line

For Requests

A request line has three parts, the method of the request, the path and a reference to the version of HTTP being used.

For example:

POST /blog/action/do_something.php HTTP/1.1

The acceptable actions are GET, POST & HEAD with GET being the most common. Method names are always written in uppercase. Similarly, the HTTP version always takes the demonstrated form.

Response

The initial response line is also known as the status line and contains the HTTP version, followed by a status code and an English “reason phrase” for the code. For example:

HTTP/1.1 301 Moved Permanently

Header Lines

Header lines provide meta-information about the request. If a message body is included they usually contain a Content-Type that returns the mime type of the body and Content-Length which returns the size of the body in bytes.

In HTTP 1.1, the Host header line is required to enable servers to differentiate between various top level domains that may reside within the server when processing requests.

Message Body

The message body contains the actual meat of the transaction. Usually this is going to be an html or xhtml document but could contain any binary or other file.

Next Time: Get Post & Head

Related Posts


Leave a Reply