Read the article: An HTTP interview will hit 80% of the interviewees
Over a year of interviews, EVERY time I asked the candidates what they knew about HTTP, I was amazed by their answers, all of whom had 3-5 years of experience. Please don’t let me call you wild coders. It’s time to learn about HTTP. Let’s be regular soldiers.
The cause of
Interviewer: Do you know anything about HTTP?
Front-end development: Shouldn’t this be clear to the back end?
Back-end development: Doesn’t the front end know this?
Interviewer:…
Let’s take HTTP in all its seriousness (it doesn’t include transport-layer TCP) and summarize the best of it.
HTTP Protocol Overview
HTTP (Hypertext Transfer Protocol) is a client/server model of communication protocol at the application layer. It consists of requests and responses and is stateless. (HTTP2 will not be introduced yet)
- agreement
The protocol defines the data transmission format that the communication parties must follow, so that the communication parties can accurately communicate according to the agreed format.
- stateless
Stateless means that there is no relationship between two connections. Each connection is a new one and the server does not record the request information before and after the connection.
- Client/server model
Five layer network model
URL form
Have you ever noticed how long it’s taken?
Content of the agreement
Request (for)
The client sends an HTTP request to the server in the following format:
- The request line
- Request header
- Request body
Response
Server response client format:
- The status line
- Response headers
- Response body
Status code
The HTTP status code consists of three decimal digits. The first decimal digit defines the type of the status code, and the second two digits do not classify. HTTP status codes are classified into five types:
classification | Classification description |
---|---|
1 * * | Message, the server receives the request and requires the requester to proceed with the operation |
2 * * | Success, the operation is received and processed successfully |
3 * * | Redirect, requiring further action to complete the request |
4 * * | Client error, request contains syntax error or request cannot be completed |
5 * * | Server error. The server encountered an error while processing the request |
For more details about the status code, see HTTP status code
But generally we only need to know a few common ones, like 200,400,401,403,404,500,502.
Request method
As of HTTP1.1, there are several methods:
methods | describe |
---|---|
GET | A GET request displays the resource specified by the request. In general, the GET method should only be used for reading data and should not be used for non-idempotent operations that have side effects. What it expects should be and should be safe and idempotent. By security, I mean that the request does not affect the state of the resource. |
POST | Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources. |
PUT | A PUT request uploads its latest content to the specified resource location. The PUT method is idempotent. This method allows the client to send the latest data of the specified resource to the server instead of the content of the specified resource. |
PATCH | PATCH method appeared relatively late, it was defined in RFC 5789 standard in 2010. PATCH requests are similar to PUT requests and are used to update resources. The differences are as follows: 1.PATCH is generally used for partial resource update, while PUT is generally used for overall resource update. 2. When a resource does not exist, PATCH creates a new resource, and PUT only updates the existing resource. |
DELETE | A DELETE request is used to ask the server to DELETE a Resource identified by the Uniform Resource Identifier (URI) requested. The specified resource is deleted after a DELETE request, and the DELETE method is idempotent. |
OPTIONS | Allows clients to view server performance. |
CONNECT | Reserved in HTTP/1.1 for proxy servers that can pipe connections. |
HEAD | Similar to a GET request, except that there is no concrete content in the response returned, which is used to retrieve the header |
TRACE | The command output displays the requests received by the server for testing or diagnosis. |
Common headers for requests and responses
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-Since For cache control |
Cache-Control | Generally, the value isno-cache ormax-age=XX , XX is an integer, indicating the cache validity period of the resource (seconds) |
Pay attention to
The content-Type of a web page defines the Type of a web file and the code of the web page, and determines the format and encoding in which the browser will read the file.
Common media format types are as follows:
Content-Type(Mime-Type) | describe |
---|---|
text/html | HTML format |
text/plain | Plain text format |
text/xml | XML format |
image/gif | GIF image format |
image/jpeg | JPG image format |
image/png | PNG image format |
Media format types beginning with Application:
Content-Type(Mime-Type) | describe |
---|---|
application/xml | XML data format |
application/json | JSON data format |
application/pdf | PDF format |
application/msword | Word Document Format |
application/octet-stream | Binary streaming data (such as common file downloads) |
application/x-www-form-urlencoded | The form data is encoded as a key/value format and sent to the server (the default form submission format). |
multipart/form-data | This format is used when you need to upload files in a form |
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-Modified Value 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-Since Along 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 |
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-Encoding Response 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 |
conclusion
Knowing this is not just a way to deal with the interviewer, it’s actually a way to improve your productivity. This is most obvious in the context of the BACKEND interface, and it’s also important to know when you’re making requests to other interfaces or forwarding requests in your application. Otherwise, the chances of problems are much higher.
Welcome to pay attention to the public number communication!