Http Messages
HTTP is based on a client/server (C/S) architecture model, exchanging information over a reliable connection. It is a stateless request/response protocol.
HTTP messages are the basis of communication between a client and a server. They consist of a series of text lines, following a specific format and structure.
HTTP messages are divided into two types: request messages and response messages.
An HTTP client is an application (web browser or any other client) that connects to a server with the purpose of sending one or more HTTP requests.
An HTTP server is also an application (usually a web service like Nginx, Apache server, or IIS server, etc.) that receives client requests and sends HTTP response data back to the client.
!(#)
* * *
## Client Request Message
An HTTP request message sent by a client to a server consists of the following format: request line, request headers, a blank line, and request data. The diagram below shows the general format of a request message.
!(#)
* **Request Line**:
* **Method**: Such as GET, POST, PUT, DELETE, etc., specifying the operation to be performed.
* **Request URI** (Uniform Resource Identifier): The path of the requested resource, usually including the hostname, port number (if non-default), path, and query string.
* **HTTP Version**: Such as HTTP/1.1 or HTTP/2.
Example format of a request line: `GET /index.html HTTP/1.1`
* **Request Headers**:
* Contains client environment information, the size of the request body (if any), compression types supported by the client, etc.
* Common request headers include `Host`, `User-Agent`, `Accept`, `Accept-Encoding`, `Content-Length`, etc.
* **Blank Line**:
* The separator between the request headers and the request body, indicating the end of the headers.
* **Request Body** (Optional):
* For certain types of HTTP requests (like 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: status line, message headers, a blank line, and the response body.
!(#)
* **Status Line**:
* **HTTP Version**: Matches the version from the request message.
* **Status Code**: A three-digit number indicating the result of processing the request, e.g., 200 for success, 404 for resource not found.
* **Status Message**: A brief description of the status code.
Example format of a status line: `HTTP/1.1 200 OK`
* **Response Headers**:
* Contains server environment information, the size of the response body, compression types supported by the server, etc.
* Common response headers include `Content-Type`, `Content-Length`, `Server`, `Set-Cookie`, etc.
* **Blank Line**:
* The separator between the response headers and the response body, indicating the end of the headers.
* **Response Body** (Optional):
* Contains the data returned by the server, such as the requested web page content, images, JSON data, etc.
* * *
## Example
The following example is a typical instance of using GET to pass data:
Client request:
GET /index.html HTTP/1.1Host: www.example.com User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0Accept: 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,508; v=b3; q=0.318 Apr 2024 12:00:00 GMT Server: Apache/2.4.1 (Unix)Last-Modified: Wed, 18 Apr 2024 11:00:00 GMT Content-Length: 12345Content-Type: text/html; charset=UTF-8 Example Page
YouTip