Introduction to HTTP

HTTP is a common protocol used to transfer data on the Web (e.g. to transfer HTML documents). It is:

  • simple it's human-readable
  • extensible thanks to HTTP headers (you can extend HTTP functionality as long as the client and the server agree on the headers' semantic)
  • based on client-server architecture i.e., the client initiates a connection to the server and waits for response
  • stateless, but not sessionless HTTP itself is stateless, but the usage of cookies allows us to create stateful sessions

Components of HTTP-based System

Components of HTTP-based systems
Components of HTTP-based systems (Source: MDN)
  • Client the user-agent (e.g. browser or bot) that initiates the request
  • Server the one that serves the response
  • Proxies systems between the client and the server (through which requests/responses pass), which can perform functions such as: caching, filtering, load-balancing, authentication, logging

Flow of HTTP

How the client initiates connection to the server:

  1. Open a TCP connection to the server
  2. Send an HTTP request to the server
  3. Read the HTTP response sent by the server
  4. Close or reuse the connection for further requests

HTTP Messages

You can debug HTTP request/response by using your browser's dev console, using external tools like Insomnia, or running command like:

# -v is for verbose
curl -v http://www.example.com/

Sample Request

GET / HTTP/1.1
Host: www.example.com
User-Agent: curl/7.64.1
Accept: */*
  • The first line ("start line") contains:
    • HTTP method e.g. GET or POST
    • Request path e.g. / or /test.html?query=alibaba
    • HTTP version e.g. HTTP/1.1
  • The next lines contain (optional):
    • HTTP Headers in the form of <headerName>: <value>
  • The next line is an empty line indicating all meta-information has been sent
  • The next line is the request body (a.k.a. the "payload", optional)

Sample Response

HTTP/1.1 200 OK
Age: 207119
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 30 May 2021 18:04:08 GMT
Etag: "3147526947+ident"
Expires: Sun, 06 Jun 2021 18:04:08 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (oxr/830D)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
  ...
</html>
  • The first line ("status line") contains:
    • HTTP version e.g. HTTP/1.1
    • Status code e.g. 200 or 404
    • Status text e.g. OK or Not Found
  • The next lines contain (optional):
    • HTTP Headers in the form of <headerName>: <value>
  • The next line is an empty line indicating all meta-information has been sent
  • The next line is the response body (a.k.a. the "payload", optional)

References