Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTTP Message Structure

HTTP Message Structure

HTTP is based on a client/server (C/S) architecture model, exchanging information over a reliable link, and is a stateless request/response protocol.

HTTP messages are the foundation of communication between client and server; they consist of a series of text lines that follow a specific format and structure.

HTTP messages are divided into two types: request messages and response messages.

An HTTP client is an application (a web browser or any other client) that connects to a server to send one or more HTTP requests to it.

An HTTP server is also an application (usually a web service such as Nginx, Apache, or IIS) that receives client requests and sends HTTP response data back to the client.


Client request message

A request message sent by the client to the server consists of the following parts: a request line, request headers, a blank line, and request data. The figure below shows the general format of a request message.

Example format of the request line:GET /index.html HTTP/1.1

- **Method**: Such as GET, POST, PUT, DELETE, etc., specifying the operation to perform. - **Request URI** (Uniform Resource Identifier): the path of the requested resource, usually including the host name, port number (if non-default), path, and query string. - **HTTP version**: Such as HTTP/1.1 or HTTP/2. - Contains client environment information, the size of the request body (if any), the compression types supported by the client, and so on. - Common request headers include `Host`, `User-Agent`, `Accept`, `Accept-Encoding`, `Content-Length`, etc. - The separator between the request headers and the request body, marking the end of the request headers. - In some 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 also consists of four parts: a status line, message headers, a blank line, and the response body.

Example format of the status line:HTTP/1.1 200 OK

- **HTTP version**: Matches the version in the request message. - **Status code**: A three-digit number indicating the result of the request, e.g., 200 means success, 404 means the resource was not found. - **Reason phrase**: A short description of the status code. - Contains server environment information, the size of the response body, the compression types supported by the server, and so on. - Common response headers include `Content-Type`, `Content-Length`, `Server`, `Set-Cookie`, etc. - The separator between the response headers and the response body, marking the end of the response headers. - Contains the data returned by the server, such as the requested web page content, images, JSON data, etc.

Example

The following is a typical example of using GET to pass 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









    Example Page





    

Hello, World!

In practical applications, HTTP messages can contain more header fields, and the sizes of the request and responses bodies vary depending on the transmitted data.