HTTP Tutorial
HTTP (HyperText Transfer Protocol) is an application-layer protocol for distributed, collaborative, hypermedia information systems.
HTTP is the foundation of data communication for the World Wide Web (WWW). It is designed to ensure communication between clients and servers, and is one of the most commonly used protocols on the Internet.
HTTP is a protocol that transfers data based on the TCP/IP communication protocol (HTML files, image files, query results, etc.).
The original purpose of designing HTTP was to provide a method for publishing and receiving HTML pages. Resources requested via HTTP or HTTPS protocols are identified by Uniform Resource Identifiers (URI).
HTTP Request-Response
The basic working principle of HTTP is that the client (usually a web browser) sends a request to the server, and after receiving the request, the server returns the corresponding resource. These resources can be web pages, images, audio files, videos, and so on.
HTTP uses a client-server model, where the client sends requests and the server returns responses.

The HTTP request-response model usually consists of the following steps:
Establish connection: The client and server establish a connection. In traditional HTTP, this is based on the TCP/IP protocol. Recent HTTP/2 and HTTP/3 use more advanced transport layer protocols, such as the binary protocol based on TCP (HTTP/2) or the QUIC protocol based on UDP (HTTP/3).
Send request: The client sends a request to the server. The request contains the URL of the resource to be accessed, the request method (GET, POST, PUT, DELETE, etc.), request headers (e.g., Accept, User-Agent), and an optional request body (for POST or PUT requests).
Process request: After receiving the request, the server finds the corresponding resource based on the information in the request and performs the corresponding processing operation. This may involve retrieving data from a database, generating dynamic content, or simply returning static files.
Send response: The server encapsulates the processed result in a response and sends it back to the client. The response contains a status code (to indicate the success or failure of the request), response headers (e.g., Content-Type, Content-Length), and an optional response body (e.g., HTML page, image data).
Close connection: After completing the request-response cycle, the connection between the client and server can be closed, unless a persistent connection is used (such as keep-alive in HTTP/1.1).
HTTP Methods
HTTP methods specify what actions a client can perform on resources on the server.
The main HTTP methods are:
- GET: Requests the specified resource from the server. This is the most commonly used method for accessing pages.
- POST: Requests the server to accept and process the data in the request body, usually used for form submissions.
- PUT: Requests the server to store a resource and replace all content of the target resource with the content in the request body.
- DELETE: Requests the server to delete the specified resource.
- HEAD: Similar to GET, but does not retrieve the content of the resource, only retrieves the response header information.
HTTP Status Codes
HTTP status codes are the server's response to a client's request.
Status codes are divided into five categories:
- 1xx (Informational status code): Indicates that the received request is being processed.
- 2xx (Success status code): Indicates that the request was processed normally.
- 3xx (Redirect status code): Further operations are required to complete this request.
- 4xx (Client error status code): Indicates that the request contains a syntax error or cannot be completed.
- 5xx (Server error status code): An error occurred on the server while processing the request.
HTTP Versions
HTTP has multiple versions. Currently widely used are HTTP/1.1 and HTTP/2, and HTTP/3 is gradually being promoted.
- HTTP/1.1: Supports persistent connections, allowing multiple requests/responses to be transmitted over the same TCP connection, reducing the overhead of establishing and closing connections.
- HTTP/2: Based on binary framing, supports multiplexing, allowing multiple, independent, bidirectional exchanges to be initiated simultaneously over a single HTTP/2 connection.
- HTTP/3: Based on the QUIC protocol, it aims to reduce network latency, improve transmission speed and security.
Security
HTTP itself is insecure because the transmitted data is not encrypted and may be eavesdropped on or tampered with. To solve this problem, HTTPS (which will be detailed in the next chapter) was introduced, i.e., adding SSL/TLS protocols to HTTP, providing encryption and authentication for data transmission.
Other extensions