Skip to content

HTTP Quick Guide

The Hypertext Transfer Protocol (HTTP) is an application-layer protocol fundamental to distributed, collaborative, hypermedia information systems. It has been the bedrock of data communication for the World Wide Web since its early days. HTTP is a versatile, stateless protocol that can be extended for various purposes through its request methods, status codes, and headers.

Essentially, HTTP is a protocol for fetching resources, such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser. A complete document is reconstructed from the different sub-documents fetched, for instance, text, layout description, images, videos, scripts, and more.

Communication between clients and servers happens by exchanging individual messages (as opposed to a stream of data). The messages sent by the client, usually a Web browser, are called requests and the messages sent by the server as an answer are called responses.

While HTTP traditionally operated over TCP/IP on port 80, modern web communication overwhelmingly uses HTTPS (HTTP Secure), which encrypts HTTP messages using TLS (Transport Layer Security) and typically operates on port 443. HTTP itself has evolved, with versions like HTTP/1.1, HTTP/2, and HTTP/3 offering significant performance improvements (e.g., connection reuse, multiplexing, header compression, and QUIC for HTTP/3).

  • HTTP is connection-oriented (but can seem connectionless): In HTTP/1.0, each request/response pair often used a separate TCP connection. HTTP/1.1 introduced persistent connections, allowing multiple requests/responses over a single connection. HTTP/2 further enhanced this with multiplexing. While connections are managed, the protocol’s request-response nature gives it a ‘connectionless’ feel per transaction.
  • HTTP is media independent: Any type of data (text, images, video, JSON, XML, etc.) can be sent via HTTP, as long as both the client and server understand how to handle the data. The Content-Type header, using MIME types, specifies the data format.
  • HTTP is stateless: Each HTTP request from a client to a server is treated independently. The server does not store any information about previous requests from the same client. This simplifies server design. To maintain state across multiple requests (e.g., for user sessions or shopping carts), techniques like cookies, tokens in headers, or server-side sessions are employed.

HTTP/1.1 enables connection reuse (persistent connections). HTTP/2 allows multiplexing requests over a single TCP connection, and HTTP/3 uses QUIC (over UDP) for even better performance, especially on lossy networks.

HTTP functions within a client-server model. A client (e.g., web browser, mobile app, command-line tool like cURL) sends an HTTP request to a server. The server (e.g., Apache, Nginx, Kestrel) processes the request and returns an HTTP response. This exchange can happen directly or via intermediaries like proxies or gateways.

Conceptual Flow:

  1. Client initiates a connection: Typically a TCP/IP connection to the server on the appropriate port (80 for HTTP, 443 for HTTPS). For HTTPS, a TLS handshake occurs first.

  2. Client sends an HTTP request: This message includes a request line (e.g., GET /index.html HTTP/1.1), headers (e.g., Host: example.com, User-Agent: browser-info), and an optional body (for methods like POST).

  3. Server processes the request: The server parses the request, locates the resource or performs the action, and prepares a response.

  4. Server sends an HTTP response: This message includes a status line (e.g., HTTP/1.1 200 OK), headers (e.g., Content-Type: text/html, Content-Length: 1234), and an optional body containing the requested resource or status information.

  5. Client receives and processes the response: The client (e.g., browser) parses the response, renders the content, or takes appropriate action.

  6. Connection management: Depending on the HTTP version and headers (e.g., Connection: close), the connection might be closed or kept alive for further requests.

The HTTP client (user agent) constructs and sends an HTTP request. This includes specifying the desired HTTP method (e.g., GET, POST), the target resource’s URI, the HTTP protocol version, various headers providing context (like acceptable content types or authentication credentials), and sometimes a message body (payload).

The HTTP server listens for incoming requests on a specific network port. Upon receiving a request, it parses it, performs the requested action (e.g., fetching a file, querying a database, executing business logic), and then sends back an HTTP response. The response includes a status code indicating the outcome, headers with metadata about the response, and often a message body with the resource representation.