HTTP Message Structure
HTTP is a client-server (C/S) architecture model that exchanges information through a reliable connection. It is a stateless request/response protocol.
HTTP messages are the foundation of communication between clients and servers, consisting of a series of text lines following a specific format and structure.
There are two types of HTTP messages: request messages and response messages.
An HTTP client is an application (such as a web browser or any other client) that sends one or more HTTP requests to a server by connecting to it.
An HTTP server is also an application (typically a web service like Nginx, Apache, or IIS) that receives client requests and sends back HTTP response data.
Client Request Message
A client sends an HTTP request to a server, which includes the following format: request line, headers, a blank line, and the request body. The general format of a request message is shown below:
- Request Line:
- Method: Specifies the action to be performed, such as GET, POST, PUT, DELETE, etc.
- Request URI (Uniform Resource Identifier): The path of the requested resource, typically including the hostname, port number (if not default), path, and query string.
- HTTP Version: The version of the HTTP protocol, such as HTTP/1.1 or HTTP/2.
Example of a request line: GET /index.html HTTP/1.1
- Request Headers:
- Include information about the client environment, the size of the request body (if any), supported compression types, etc.
- Common headers include Host, User-Agent, Accept, Accept-Encoding, Content-Length, etc.
- Blank Line:
- A separator between the headers and the body, indicating the end of the headers.
- Request Body (Optional):
- In certain types of HTTP requests (such as POST and PUT), the request body contains the data to be sent to the server.
Server Response Message
An HTTP response consists of four parts: status line, headers, a blank line, and the response body.
- Status Line:
- HTTP Version: Matches the version in the request message.
- Status Code: A three-digit number indicating the result of the request, such as 200 for success or 404 for not found.
- Status Message: A short description of the status code.
Example of a status line: HTTP/1.1 200 OK
- Response Headers:
- Include information about the server environment, the size of the response body, supported compression types, etc.
- Common headers include Content-Type, Content-Length, Server, Set-Cookie, etc.
- Blank Line:
- A separator between the headers and the body, indicating the end of the headers.
- Response Body (Optional):
- Contains the data returned by the server, such as the requested webpage content, images, JSON data, etc.
Example
Here is a typical example of using GET to transmit data:
Client Request:
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Connection: keep-alive
Server Response:
HTTP/1.1 200 OK
Date: Wed, 18 Apr 2024 12:00:00 GMT
Server: Apache/2.4.1 (Unix)
Last-Modified: Wed, 18 Apr 2024 11:00:00 GMT
Content-Length: 12345
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>Hello, World!</h1>
<!-- The rest of the HTML content -->
</body>
</html>