HTTP message structure and content


HTTP message content

First, understand the following HTTP message contents through mind map

HTTP message structure and content

The format of HTTP request message is as follows:

HTTP message structure and content

HTTP message structure and content

1.1 request line

The request line consists of three parts: request method, request URL (excluding domain name), and HTTP protocol version
The most commonly used isGETandPOST

1.1.1 request method

  • 1、GET
Request to obtain the resource identified by the request URI (URI: uniform resource identifier, URL: uniform resource locator, which is a subset of it. The URI focuses on identification, while the URL emphasizes location and can be regarded as the original URI).
The length of the passed parameters is limited, so the passed parameters are directly expressed in the address bar, and the length of the URL is limited by specific browsers and servers.
Therefore, get is not suitable for transmitting private data or large amounts of data.
Most general HTTP requests are get.
  • 2、POST
Add new data after the resource identified by the request URI. Post encapsulates the transmitted data in the HTTP request data in the form of name / value. It can transmit a large amount of data. There is no restriction on the data and will not be displayed in the URL.
The form is submitted using post.
  • 3、HEAD
Head is similar to get, but when the server receives the head request, it only returns the response header and does not send the response content. Therefore, if you need to view the status of a page, using head is more efficient because it saves the time of transmitting the page content.
  • 4、DELETE
Delete a resource and request to delete the resource pointed to by the URL
Request to query the performance of the server or query the options related to resources to obtain the methods supported by the current URL. If the request succeeds, a header named "allow" will be included in the HTTP header, and the value is the supported methods, such as "get, post"
  • 6、PUT
Put a resource in a specified location.
In essence, put and post are very similar. They both send data to the server, but there is an important difference between them. Put usually determines the storage location of resources, while post does not. The storage location of post data is determined by the server itself.
On the difference between post and put and the idempotency of request methods
  • 7、TRACE
Tracking the firewall, proxy or gateway that the request passes through is mainly used for testing or diagnosis
Reserved for future use. The connect method is a proxy server reserved for HTTP / 1.1 protocol and can change the connection to pipeline. Typically used for communication between SSL encrypted server links and unencrypted HTTP proxy servers.

1.1.2 HTTP protocol version

  • 1、HTTP/1.0
Http / 1.0 supports three request methods: get, post and head.
  • 2、HTTP/1.1
Http / 1.1 is the version currently in use. This version adopts persistent connection by default, and can work well with the proxy server. It also supports sending multiple requests simultaneously in the form of pipeline, so as to reduce the route load and improve the transmission speed.
Http / 1.1 adds five HTTP request methods: options, put, delete, trace and connect
  • 3. Request header
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Connection: keep-alive
Content-Length: 18
Content-Type: application/json;charset=UTF-8
Origin: http://localhost:1234
Referer: http://localhost:1234/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44
The request header consists of keywords / values, one pair per line
1. User agent: the type of browser that generates the request
2. Accept: the type of data that the client wants the browser to accept, such as accept: application / JSON, text / plain, * / * indicates that it wants to accept JSON and text types, * / * indicates that it accepts all types.
3. Content type: the type of entity data sent by the sender, such as content type: application / JSON; Charset = UTF-8, which means JSON format data.
4. Host: the requested host name, which allows multiple domain names to be located at the same IP address, that is, the virtual host
  • Content-Type
    Common content type:
Content-Type explain
text/html html format
text/plain Plain text format
text/css CSS format
text/javascript JS format
image/gif Gif picture format
image/jpeg Jpg picture format
image/png Png picture format
application/x-www-form-urlencoded Post specific: This is the default method for normal form submission. The form data is encoded in key / value format and sent to the server.
application/json Post specific: used to tell the server that the message body is a serialized JSON string
text/xml Post dedicated: send XML data
multipart/form-data Special for post: it will be explained below
  • Blank line
The request header is followed by an empty line, notifying the server that there are no more request headers below
  • Request body
Get does not request data, post does.
The most commonly used request headers related to request data are content type and content length.

response message

The structure of HTTP response message is similar to that of request message, which is also composed of four parts:

< status line > // status line

< headers > // message header

< blank line > // blank line

< response body > // response body
  • Status line
The status line also consists of three parts: the Server HTTP protocol version, the response status code, and the text description of the status code

  Format: http version status code reason phase CRLF

  For example: http / 1.1 200 OK

Status code: it consists of 3 digits. The first digit defines the category of response

1XX: indicates that the request has been received and processing continues
2XX: successful, indicating that the request has been successfully accepted and processed.
200 OK: the client request succeeded
204 no content: no content. The server processed successfully, but did not return content. It is generally used when only the client sends information to the server, but the server does not return any information to the client. The page will not be refreshed.
206 partial content: the server has completed some get requests (the client has made a range request). The response message contains the entity content within the specified range of content range
3xx: redirection
301 moved permanently: permanently redirected, indicating that the requested resource has been permanently moved to another location.

302 found: temporary redirection, indicating that the requested resource has been temporarily moved to another location

303 see other: for temporary redirection, get orientation should be used to obtain the requested resources. 303 has the same function as 302, except that 303 specifies that the client should use get access

307 temporary redirect: temporary redirect, which has the same meaning as 302. Post does not become get

304 not modified: indicates that when the client sends a conditional request (if... In the get method request message), the condition is not satisfied. When 304 is returned, no response body is included. Although 304 is divided into 3xx, it has nothing to do with redirecting a dime
A usage scenario:
When the cache server requests a resource from the server, the response message returned by the server has such a field: last modified: wed, 7 SEP 2011 09:23:24. The buffer will save the resource and its last modification time at the same time. The next time the user requests this resource from the buffer, the buffer needs to make sure that the resource is new. Then it will send an HTTP request (get method) to the original server and include a field in the request header: if modified since: wed, 7 SEP 2011 09:23:24. This value is the last modification time in the response message sent by the server last time.

Assuming that this resource has not been modified, the server returns a response message:

HTTP/1.1 304 Not Modified
    Date:Sat, 15 Oct 2011 15:39:29
    (blank line)                                      
    (empty responder)

Use 304 to tell the buffer that the resource has not been modified, and the response body is empty, which will not waste bandwidth.

4xx: client error
    -400 bad request: the client request has syntax error and the server cannot understand it.
    -401 unauthorized: the request is unauthorized. This status code must be used with the WWW authenticate header field.
    -403 Forbidden: the server received the request but refused to provide the service
    -404 not found: the requested resource does not exist. For example, you entered the wrong URL
    -415 unsupported media type: Unsupported media type
5xx: server side error, the server failed to implement the legal request.
    -500 Internal Server Error: unexpected error occurred in the server.
    -503 server unavailable: the server cannot process the client's request at present. It may return to normal after a period of time,

This work adoptsCC agreement, reprint must indicate the author and the link to this article