This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

HTTP request and response

Now, let us discuss the contents of both an HTTP request and an HTTP response.

HTTP request

An HTTP request could look like this:

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0

The first line is a request line. GET is the request method, which tells the server that the client wants to retrieve something. That something is the second item /index.html (the resource). This essentially tells the server to retrieve the file /index.html (the resource) from the server.

Other commonly used HTTP request methods:

  • POST: Like GET, except you also pass some parameters in the optional message body. This is often used to create a new resource.
  • PUT: Tells the server to store (in the specified resource) some parameters specified in the message body
  • DELETE: Tells the server to delete the specified resource

The subsequent lines form the request header. Host is a compulsory header to point to the server. Most of the other possible request header fields are less relevant. I am showing you User-Agent in the example simply to demonstrate how the server knows what browser you are using.

There can also be an optional message body, which can be used for example by the methods POST and PUT above.

HTTP response

An HTTP response could look like this [taken from Wikipedia]:

HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 155
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
ETag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Connection: close

<html>
  <head>
    <title>An Example Page</title>
  </head>
  <body>
    <p>Hello World, this is a very simple HTML document.</p>
  </body>
</html>

The first line HTTP/1.1 200 OK is a status line, where the server responses with a status code (200) and a message (OK). Code 200 is when everything is successful. You might have encountered other codes in your web browsing lifetime: 404 (Not found), 403 (Forbidden), 500 (Internal Server Error), 503 (Service Unavailable). You can see here for a list of HTTP status codes.

Like an HTTP request, the subsequent lines (up to an empty line) form the response header. Most of them are quite self-explanatory. I would like to draw your attention to the line Content-Type: text/html; charset=UTF-8. The server basically tells the client to expect the response to be some HTML code. The response could have also been some other type: a plain text, a JSON string, a PDF, an image, an audio, a zip file etc.

The optional message body (after the empty line) gives the content of the response, in this case the HTML code.