Please indicate the source of reprint by: @kidou The content of this article is limited to the content I am familiar with, HTTP content any knowledge can write a blog alone, so don’t expect to rely on a blog can tell clearly, the main role of this article is to pave the way for the future blog, so more detailed HTTP protocol content can refer to RFC 2616, my level is limited, If there are incorrect places welcome to point out the message.

Main contents of this paper:

  • Format of the HTTP request packet
  • Format of the HTTP response packet
  • Header
  • Three forms of the request body
  • Recommended Debugging tools
  • HTTP composition diagram

1. Format of the HTTP request packet

HTTP request packets are divided into three parts: request line, request header, and request body. The format is shown as follows:


Format of the HTTP request packet


Note: Some articles also divide HTTP request packets into two parts: request header and request body. The first behavior of the request header is the request line.

1.1 request line

The Request Line is divided into three parts: Request method, Request address, and protocol and version, ending with CRLF(\ R \n). HTTP/1.1 defines eight request methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, and TRACE. The two most common GET and POST methods are used for RESTful interfaces.

Before we look at the request address, let’s look at the composition of the URL:




URL.png


The PATH is the part of the URL host that contains the Query String, as in:

URL PATH
http://http://www.jianshu.com/ /
http://localhost:8080/index.php?id=1234 /index.php? id=1234
Weibo.com/920507888/h… /920507888/home

Example request message:




Example of HTTP request packet format

2. Format of the HTTP response packet

The format of the HTTP response is the same, except that the status line (the first line) is not the same as the request line, but the status line is not the same as the request line.


.http_ response

For details about the status code (the response code in the figure above), see the HTTP status code

To visualize the HTTP response, the following example, between NC 1270.1:80 << EOF and EOF, is a simple HTTP request.


HTTP requests and responses

3. Header

Header can be used to pass some additional information in the format: key: value, note that there is a space after the colon! Such as:

Content-Length: 1024
Content-Type: text/plain
Copy the code

3.1 Common Common Headers for Request and Response

The name of the role
Content-Type Type of the request body or response body, for example, Text /plain or Application/JSON
Accept Description received type, can be multiple values, with.(Half corner comma) separate
Content-Length The length of the request/response body, in bytes
Content-Encoding Request/response body encoding format, such as Gzip, DEFLate
Accept-Encoding Tell them the content-encoding we accept
ETag Identifies the current resource, andLast-Modified,If-None-Match,If-Modified-SinceFor cache control
Cache-Control Generally, the value isno-cacheormax-age=XX, XX is an integer, indicating the cache validity period of the resource (seconds)

3.2 Common Request Headers

The name of the role
Authorization Set the identity authentication information
User-Agent User id, such as OS and browser type and version
If-Modified-Since The value is returned by the previous serverLast-ModifiedValue to confirm if a resource has been changed and read from the cache if it has not (304)
If-None-Match The value is the ETag value returned by the server last timeIf-Modified-SinceAlong with them
Cookie The existing cookies
Referer Represents the address from which the request is referenced, such as the address of page A when you jump from page A to page B
Host The requested host and port number

3.3 Common Response Headers

The name of the role
Date Date of the server
Last-Modified When the resource was last modified
Transfer-Encoding The value is generally chunked. If content-Length is not specified, it indicates that the server does not know the data size of the response bodyContent-EncodingResponse headers
Set-Cookie Set the Cookie
Location Redirect to another URL, as in browserbaidu.comPress enter, it will automatically jump towww.baidu.comIs controlled by the response header
Server Background server

4. Three forms of request body

The request body of an HTTP request can take three different forms depending on the application scenario.

The first:

As is common among mobile developers, the request body is of any type, the server does not parse the request body, and the processing of the request body needs to be resolved itself, such as when Posting JSON.




HTTP request, form 1

The second:

The second and third, both of which have fixed formats, are the first two that server-side developers are aware of. The format requirements are the same as the format requirements of Query String in URL: Multiple key-value pairs are joined with & and = precedes the key and value. Only ASCII characters can be used. Non-ascii characters must be encoded with UrlEncode.


HTTP request, form two

Second example

The third:

The request body of the third Type of request body is divided into multiple parts, which will be used for file uploading. This format should first be used for email transmission. Each field/file is divided into separate sections by boundary (specified in content-Type). The blank line is followed by the content after the description header, and the mark of the end of the request is marked as boundary followed by –, the structure is shown in the following figure:


HTTP request, form three

The key to distinguishing content-disposition from being treated as a file is whether content-disposition contains filename. Because files have different types, content-type is also used to indicate the Type of file. If you do not know the Type, the value can be application/octet-stream to indicate that the file is a binary file. If it is not a file, the content-type can be omitted.

Here is an upload request with a file:




A third request body example

Application/X-ww-form-urlencoded request header (application/ X-ww-form-urlencoded request header); Content-Type (Application/X-ww-form-urlencoded request header); multipart/form-data; Boundary ={boundary}, * The above form-data can also be mixed, alternative, digest, parallel, but I only use form-data. If the two do not match, then the server will not parse the request body. In other words, it’s only the first case!

Forms or mock forms refer to the second and third types (multipart/form-data).

5. Debugging tools are recommended

5.1 the cURL

CURL is a powerful command line tool that supports almost all of the protocols you know about. CURL is a powerful command line tool that supports almost all of the protocols you know about. CURL is a powerful command line tool that supports almost all of the protocols you know about.


The cURL screenshots

5.2 bat

Bat is a command line API debugging tool similar to cURL developed by Astaxie with Golang. It can easily print HTTP requests and responses, highlight headers, format JSON, and other functions. It is very easy to use, and is a magic tool for API modding.


Bat screenshots

5.3 nc

Nc is netcat shorthand, known as the “Swiss army knife of networking tools”, but personally, I regard it as a Socket to use, often use it to print all kinds of requests, its role is not only that, of course, you can also use it to launch all kinds of requests, mode POP3 is also used it before, just request message to write their own, The graph above “HTTP requests and responses” is done with NC.

6. HTTP composition diagram

I feel it is a little messy. Would it be better to write it according to the structure below?


The HTTP packet consists of. PNG