Skip to content
UoL CS Notes

HTTP Messages

COMP211 Tutorials

HTTP

There are two types of HTTP messages:

  • request
  • response

HTTP messages are encoded in ASCII. Here is one now:

GET / HTTP/1.1
Host: comp211.gairing.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-GB
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1

Only the GET and Host: lines are required for a valid request.

  • The top line is the request line. You can make requests such as GET, POST and HEAD.
  • The rest of the lines are called the header lines. They specify certain parameters that the browser can set.

All lines end with a \r\n to indicate a carriage return and line feed. There is also an extra one at the end of the header lines.

HTTP Request Message Format

<method> <URL> <version>\r\n	# request line
<header field name> <value>\r\n	# header lines
...
\r\n
<entity body>	# the requested body

Other HTTP Request Messages

POST method:

  • Web page often includes form input.
  • User input sent from client to server in entity body of HTTP POST request message.

GET method (for sending data to server):

  • Include user data in URL field of HTTP GET request message.

    This follows a ? in the URL.

HEAD method:

  • Requests headers (only) that would be returned if specified URL were requested with an HTTP GET method.

PUT method:

  • Uploads new file (object) to the server.
  • Completely replaces file that exists at the specified URL with content in the entity body of POST HTTP request message.

    This method is often blocked on web servers.

HTTP Response

This is the response that the server sends back to a client:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 417
Connection: keep-alive
Keep-Alive: timeout=15
Date: Mon, 04 Oct 2021 11:40:11 GMT
Server: Apache
Last-Modified: Thu, 03 Oct 2019 20:28:35 GMT
ETag: "1a1-594076e0ed808"
Accept-Ranges: bytes


<HTML>
<HEAD>
<TITLE>This is just a simple HTML page</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<HR>
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href="mailto:support@yourcompany.com">
support@yourcompany.com</a>.
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold
italics.</I></B>
<HR>
</BODY>
</HTML>
  • The top line is the status line.
  • Next are header lines.
  • Then the data requested.

HTTP Response Status Codes

Code Definition
200 OK Request succeeded, requested object later in this message.
301 Moved Permanently Requested object moved, new location specified later in this message. (In Location: field.)
400 Bad Request Request msg not understood by server.
404 Not Found Requested document not found on this server.
505 HTTP Version Not Supported