There are two types of HTTP packets: request packets and response packets
1. Request message
An HTTP request packet consists of four parts: request line, header, blank line, and request data.
The following figure shows the general format of the request message.
A simple example
POST /user HTTP/1.1 // Request lineHost: www.user.com
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
User-agent: Mozilla / 5.0. / / the above is request header (here must have sold out line | / / short line segment header and request content name = world / / request body (optional, such as when the get request optional)Copy the code
1.1 request line
Request consists of three parts: methods, the request URL (do not include domain name |, the HTTP protocol version
There are many request methods: GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE and CONNECT
The most common ones are GET and POST.
1.1.1 Request method
1) GET
The length of the passed parameter is limited because the passed parameter is represented directly in the address bar, and specific browsers and servers have restrictions on the length of the URL.
Therefore, GET is not suitable for transferring private data or large amounts of data.
The average HTTP request is mostly a GET.
2) POST
POST encapsulates the data to be transferred in HTTP request data in the form of names and values. It can transfer a large amount of data without limitation and will not be displayed in the URL. The form is submitted using POST.
3) the HEAD
HEAD is similar to GET, but when a server receives a HEAD request, it returns only the response header, not the response content. So, if you only need to view the status of a page, HEAD is more efficient because it saves time transferring the content of the page.
4) the DELETE
Delete a resource.
5) OPTIONS
Used to get the method supported by the current URL. If the request is successful, the HTTP header contains a header named “Allow” with the value of the supported method, such as “GET, POST”.
6) PUT
To store a resource in a specified location.
In essence, PUT and POST are very similar in that they send data to the server, but there is one important difference. PUT usually specifies where resources should be stored. POST does not.
7) TRACE
The command output displays the requests received by the server for testing or diagnosis.
8) CONNECT
The CONNECT method is reserved for HTTP/1.1 and enables the connection to be piped to a proxy server. Typically used for communication between SSL encrypted server links and unencrypted HTTP proxy servers.
1.1.2 HTTP version
1) the HTTP / 1.0
HTTP/1.0 supports GET, POST, and HEAD HTTP request methods.
2) HTTP / 1.1
HTTP/1.1 is the version currently in use. This version uses persistent connections by default and works well with proxy servers. Multiple requests can also be piped simultaneously to reduce line load and increase transmission speed.
HTTP/1.1 added five HTTP request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT.
1.2 Request Headers
The request header consists of keyword/value pairs, one per line
Typical request headers are:
● User-agent: type of browser that generates the request.
● Accept: list of response content types recognized by the client; The asterisk “*” is used to group types by scope, with “/” indicating all acceptable types and “type/*” indicating all subtypes of acceptable Type; For example, Accept: text/ XML (application/json) indicates that you want to receive XML (json).
Accept-language: natural Language acceptable to clients;
● Accept-encoding: Encoding compression format acceptable to the client;
● Accept-charset: acceptable character set for replies;
● Host: the requested Host name, allowing multiple domain names at the same IP address, that is, virtual Host;
● Connection: Connection (close or Keepalive);
● Cookie: stored in the client extension field, to the server of the same domain name to send cookies belonging to the domain;
● Content-Type: indicates the data Type of entity data sent by the sender. For example, content-type: text/ HTML (application/json) indicates that the Type sent is HTML.
The figure above shows the attributes of a generic request
1.2.1 the 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 image format |
image/jpeg | JPG image format |
image/png | PNG image format |
application/x-www-form-urlencoded | POST specific: Normal form submission is done this way by default. 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 only: sending XML data |
multipart/form-data | POST only: explained below |
Multipart /form-data supports sending binary data to the server for file uploading in POST requests
Now use Postman to send baidu a multipart/form-data POST packet. The request packet looks like this:
POST / HTTP / 1.1
Host: www.baidu.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 033120fe-2185-15d4-e486-75e86e2baddd
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="url"
https://www.baidu.com/
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="name"
waffle
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="desk"; filename="Table. JPG"
Content-Type: image/jpeg
...contentsOf the table. JPG... ------WebKitFormBoundary7MA4YWxkTrZu0gW--Copy the code
Where, the parameter boundary means the boundary.
Each argument is separated by a boundary, and there is a blank line between the argument name (binary data also needs to indicate the file type) and the argument value. This blank line cannot be omitted:
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="url"
https://www.baidu.com/
Copy the code
The message body ends with a –boundary– identifier.
See Multipart/form-data for a more detailed explanation
1.3 a blank line
The request header is followed by an empty line notifying the server that there are no more headers below
1.4 request body
GET does not request data, POST does.
The most commonly used request headers associated with request data are content-Type and Content-Length.
2. Response message
The STRUCTURE of an HTTP response packet is similar to that of a request packet and consists of four parts:
2.1. Status lines
The status line 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-phrase CRLF
For example: HTTP/1.1 200 OK
2.1.1 Status code: consists of three digits. The first digit defines the category of the response
1xx: indicates that the request has been received and processing continues
2xx: Succeeded: The request is successfully accepted and processed.
- 200 OK: The client request is successful
- 204 No Content: No Content. The server processed successfully, but did not return content. This is usually used when the client sends information to the server, but the server does not return any information to the client. The page is not refreshed.
- 206 Partial Content: The server has completed a Partial GET request (the client made a scope request). The response packet contains the entity Content in the Range specified by content-range
3xx: redirection
- 301 Moved Permanently: Permanently redirected: Indicates that the requested resource has been Permanently Moved to another location.
- 302 Found: Temporary redirection, indicating that the requested resource was temporarily moved to another location
- 303 See Other: indicates temporary redirection. Use GET to obtain requested resources. 303 functions the same as 302, except that 303 specifies that the client should use GET access
- 307 Temporary Redirect: indicates Temporary redirection, which has the same meaning as 302. POST doesn’t become GET
- 304 Not Modified: Indicates that the client sends a conditional request (IF in the GET request packet). , the condition is not met. When 304 is returned, no response body is included. Although 304 is classified in 3XX, it has nothing to do with redirection
In the scenario of 304, when the cache server requests a resource from the server, the response packet returned by the server has the following field: Last-Modified:Wed,7 Sep 2011 09:23:24. The cache saves the resource and its Last modification time. The next time a user requests this resource from the cache, and the cache needs to determine that the resource is new, it sends an HTTP request (GET method) to the original server with a field in the request header: If-modified-since :Wed,7 Sep 2011 09:23:24, the value is the last Modified time in the response packet sent by the server last time.
Assuming the resource has not been modified, the server returns a response:
HTTP/1.1 304 Not Modified Date: Sat, 15 Oct 2011 15:39:29 (blank line)Copy the code
Use 304 to tell the cache that the resource has not been modified and that the response body is empty, not wasting bandwidth.
4xx: Client error
- 400 Bad Request: The client Request has a syntax error that the server cannot understand.
- 401 Unauthorized: The request is not authorized. This status code must be used with the WWW-Authenticate header field.
- 403 Forbidden: The server receives requests but refuses to provide services
- 404 Not Found: The requested resource does Not exist. For example, enter the wrong URL
- 415 Unsupported media type: indicates the Unsupported media type
5xx: a server error. The server failed to fulfill a valid request.
- 500 Internal Server Error: An unexpected Error occurs on the Server.
- 503 Server Unavailable: The Server cannot process requests from clients. However, the Server may become normal after a period of time
2.2 Response header (header line) : after the status line of the response message
Date header: the time when the message was generated
Age header :(since initial creation) duration of response
Server header: Identifies the Server program name and version to the client
ETage header: Opaque verifier
Location header: Alternate Location of the URL
Content-length Header: the Length of the entity
Content-tyep header: Media type of the entity
Negotiation header: accept-ranges: the types of Ranges the server can Accept for the current resource Vary: header list, the server selects the most appropriate version from the list and sends it to the client in the security-related response header: set-cookie: The server sends a token www-authentication to a client on its first request, which requires the client to provide the account and password
The response header usually contains the following content:
2.3 Response entity: located after the response head (first line)
The entity contains the object requested by the Web client. The Content-Length and Content-Type headers are used to calculate the location, data Type, and data Length of the entity. After receiving the request packet from the Web client, the Web server parses the HTTP request packet, packages the request object from the Web client, and sends the data back to the Web client through the HTTP response packet. If an error occurs, the Web server returns an HTTP response packet containing the error code and the error cause.
Source: HTTP request packets and response packets Detailed description of HTTP packets