HTTP Tutorial
HTTP (HyperText Transfer Protocol) is an application-layer protocol for distributed, collaborative, hypermedia information systems.
HTTP is the foundation of data communication on 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 communication protocol based on TCP/IP for transmitting data (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 the HTTP or HTTPS protocol are identified by Uniform Resource Identifiers (URIs).
HTTP request-response
The basic working principle of HTTP is that a client (usually a web browser) sends a request to a server, and the server returns the corresponding resource after receiving the request. These resources can be web pages, images, audio files, videos, etc.
HTTP uses a client-server model, in which the client sends requests and the server returns responses.

The HTTP request-response model usually consists of the following steps:
-
Establish a connectionA connection is established between the client and the server. In traditional HTTP, this is based on the TCP/IP protocol. The newer HTTP/2 and HTTP/3 use more advanced transport-layer protocols, such as the TCP-based binary protocol (HTTP/2) or the UDP-based QUIC protocol (HTTP/3).
-
Send the requestThe client sends a request to the server; the request contains the URL of the resource to access, 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 the requestAfter receiving the request, the server finds the corresponding resource based on the request information and performs the appropriate processing. This may involve retrieving data from a database, generating dynamic content, or simply returning a static file.
-
Send the responseThe server packages the processed result into a response and sends it back to the client. The response contains a status code (indicating success or failure), response headers (e.g., Content-Type, Content-Length), and an optional response body (e.g., an HTML page or image data).
-
Close the ConnectionAfter the request-response cycle completes, 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 the client can perform on resources on the server.
The main HTTP methods are:
- GETRequest the specified resource from the server. This is the most commonly used method, for accessing pages.
- POSTAsk the server to accept and process the data in the request body, commonly used for form submission.
- PUTAsk the server to store a resource and replace all content of the target resource with the request body content.
- DELETEAsk the server to delete the specified resource.
- HEADSimilar to GET, but does not fetch the resource content, only the response header information.
HTTP Status Codes
HTTP status codes are the server's response to client requests.
Status codes are divided into five classes:
- 1xx (Informational)Indicates the received request is being processed.
- 2xx (Success)Indicates the request was processed normally.
- 3xx (Redirection)Further action is needed to complete the request.
- 4xx (Client Error)Indicates the request contains a syntax error or cannot be completed.
- 5xx (Server Error)The server encountered an error while processing the request.
HTTP version
HTTP has multiple versions; currently the widely used ones are HTTP/1.1 and HTTP/2, with HTTP/3 gradually being adopted.
- HTTP/1.1Supports persistent connections, allowing multiple requests/responses to be transmitted over the same TCP connection, reducing the overhead of establishing and closing connections.
- HTTP/2Based on binary framing, supports multiplexing, allowing multiple, independent, bidirectional exchanges to be initiated simultaneously over a single HTTP/2 connection.
- HTTP/3Based on the QUIC protocol, designed to reduce network latency and improve transmission speed and security.
Safety
HTTP itself is insecure because the transmitted data is unencrypted and may be eavesdropped on or tampered with. To solve this problem, HTTPS was introduced (explained in detail in the next chapter), which adds the SSL/TLS protocol on top of HTTP, providing encryption and authentication for data transmission.