be a student, not a follower

HTTP Request Response Basics

Apr 9th, 2013

At the most basic level, the internet works like two kids (a boy and a girl) in a class room passing notes between each other.

First, the boy writes down his question on a small piece of paper, folds, it up, labels it to the girl, and passes it down the isle. The note hops from person to person until it reaches the girl for whom it was intended. The girl then takes out a small piece of paper and replies to the boys question and delivers it back to him in a similar manner.

Similarly, when you want to get information from the internet, you create a small message, label it to a server, and send it out. It hops from computer to computer until it reaches the server. The server opens the message, reads it, and then responds in a similar manner.

Because computers aren’t as intelligent as people, we have to define the exact format of the message to be sent. This ensures the computer will know how to properly read the information contained in the message. The HTTP protocol defines this message format.

In HTTP communication there is always a request and a response. Like our example above, the boy requests something of the girl and she responds. Likewise, a web browser will make a request and a server will respond.

1) Request Line – This is the first line of the request. It specifies the request method (GET, POST, PUT, DELETE, etc), the resource, and the protocol used. It ends with CRLF (that is, a carriage return character followed by a line feed character)

2) Headers – Next are the headers. This is a way to pass information to the server about the request. There is one header per line. Each line ends with a CRLF.

3) Body (optional) – Information can also be sent the the server in body. The body is distinguished from the headers by an empty line. In the sample request above there is not body.

The HTTP protocol also specifies the format of the response from the server to the client. The response format is very similar to the request format.

1) Status Line – This is the first line of the response. It specifies the the protocol used and the http response code. It ends with CRLF

2) Headers – Next are the headers. This is a way to pass information to the client about the response. There is one header per line. Each line ends with a CRLF.

3) Body (optional) – Information can also be sent the the client in body. The body is distinguished from the headers by an empty line.

When doing web development, it helps to understand this concept. When trouble shooting problems with your web site you can use various tools to examine the client request and server response. My particular favorite are the web developer tools in the Google Chrome web browser. By opening the developer tools you can view the network tab to examine requests and responses.