This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: HTTP

HTTP response

face Josiah Wang

Once you have sent your HTTP request to a server, the server will process your request and give you an 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), etc. 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. Take a look at the third line: "Content-Type: text/html; charset=UTF-8". The server is telling 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 source code itself.