[Interface test] HTTP protocol

1. HTTP protocol basics

Introduction to HTTP

HTTP is a standard for client-side (user) and server-side (website) requests and responses (TCP). Usually the client initiates a request and creates a TCP connection to the server. When the server listens to the client's request, it will return a status and content to the client. As shown in the figure below, it is a simple process in which the client initiates a request and the server responds.

How HTTP works 

How HTTP works mainly involves the interaction between the client and the server.

  1. When we enter the URL in the browser and press Enter, the browser will send an HTTP request to the server. This request consists of multiple parts, including the request line, headers, and body. The request line contains the request method, request URI and HTTP version number, such as GET, POST, PUT, DELETE, etc. The request URI specifies the location of the resource to be accessed. The HTTP version number indicates the HTTP protocol version used by the request. The header contains a series of key-value pairs used to pass some request information to the server. For example, Accept indicates the response content type that the client can accept, User-Agent indicates the client's browser information, Referer indicates which page the client jumps from, etc. The body is optional and is used to pass some data to the server. For example, when a client submits a form to the server, the form data can be placed in the body of the request.
  2. After the server receives the request message, it generates a server response message based on the URL of the request message and the corresponding processing logic, and sends it to the client. The response message contains information such as protocol version, status code, response header, and response body. The status line contains the HTTP version number, status code, and status phrase. The HTTP version number indicates the HTTP protocol version used in the response. The status code is a three-digit number used to indicate the server's processing result of the request, such as 200 indicating success, 404 indicating resource not found, 500 indicating internal server error, etc. The status phrase is a short description of the status code. For example, the status phrase corresponding to 200 is OK, and the adverbial phrase corresponding to 404 is Not Found. The header contains a series of key-value pairs used to deliver some response information to the client. For example, Content-Type indicates the type of response content, Content-Length indicates the length of response content, Set-Cookie indicates that the server requires the client to save a cookie, and so on. The body is the actual content of the response. For example, when a client requests a web page, the HTML code of the web page can be placed in the body of the response.
HTTP features 

  1. Support client/server mode: HTTP protocol supports client/server mode, where the client initiates a request to the server and receives a response.
  2. Simple and fast: The HTTP protocol is simple and fast. When the client sends a request to the server, it only needs to transmit the request method and path, and the response is relatively fast.
  3. Flexible: HTTP allows the transmission of any type of data object and is flexible, which means that it can not only transmit text, but also non-text data such as images, audio, and video.
  4. Connectionless: The HTTP protocol is connectionless, which means that each connection is limited to processing only one request. After the server processes the client's request and receives the client's response, it disconnects the connection to save resources.
  5. Stateless: The HTTP protocol is stateless, and each request is independent, which means that the server does not save the association between previous and current requests and responses.
HTTP 和 HTTPS

The working principles and characteristics of HTTP are introduced above. It seems to be a very good protocol, but HTTP also has many shortcomings, especially in terms of security. HTTP communication uses plain text transmission, and the identity of the communicating party is not verified during the communication, and the integrity of the message cannot be proven. Therefore, using HTTP communication may cause the content to be eavesdropped, the identity of the communicating party to be disguised, and the message to be tampered with.

There is no encryption mechanism in the HTTP protocol, but it can be used in combination with SSL (Secure Socket Layer) or TLS (Transport Layer Security) to encrypt HTTP communication content. That is, HTTPS appears. The S in HTTPS can be understood as Secure, which is an HTTP channel aimed at security. You can understand HTTPS = HTTP + encryption + authentication + integrity protection.

Careful students will find that some large websites, especially banking, payment and other websites, use HTTPS because it is more secure.

2. Introduction to HTTP protocol

1.URL

URL: It is the address of a standard resource on the Internet, generally called a Uniform Resource Locator.
Composition: protocol://hostname[:port]/path/[? query parameter 1 & query parameter 2]

like:

2.HTTP protocol

HTTP: Hypertext Transfer Protocol, an application layer protocol based on requests and responses.
Function: It stipulates the information transfer specifications between the client and the server, and is a protocol that both parties abide by.
composition:
  • HTTP request: Define request data format      request line , request header , request body
  •  HTTP response: Define response data format    status line , response header , response body 

Let's take the St. Regis takeout project as an example. We sent a request on the login interface and held down F12 to debug:

1. Request line

Position: first row of requested data
Function: Describes the request method , accessed resources , and protocol version

The request line is divided into three parts: request method, request address URL and HTTP protocol version, separated by spaces. For example, in the figure below: POST /employee/login HTTP/1.1.

Request method

There are 8 request methods defined in HTTP/1.1, namely GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, and TRACE. GET and POST are the two most common request methods. If it is a RESTFul interface, it is GET, POST, DELETE, and PUT.

GET : Get resources from the server
POST : Create a new resource on the server
PUT : Update resources on the server
DELETE : Delete resources from the server

Protocol version

The format of the protocol version is: protocol name/major version number.minor version number. Common ones are HTTP/1.0 and HTTP/1.1.

2. Request header

Position: between the second line of the requested data and the blank line

Function: Notify the server that the client requests information

Features: The request header consists of key-value pairs , one pair per line
Content-Type: request body data type
  • text/html: HTML format
  • image/jpeg: jpg picture format
  • application/json : JSON data format
  • application/x-www-form-urlencoded : The default submission data format of the form
  • multipart/form-data: used when uploading files in forms

3. Request body

 

4. Response line 

Position: first line of response data
Function: Describe server processing results
Content: The status line consists of protocol version number, status code , and status message

It is divided into three parts: HTTP protocol version, status code and status code description, separated by spaces. For example  HTTP/1.1 200 OK.

  • HTTP protocol version: consistent with the protocol version in the request line. The format is: protocol name/major version number.minor version number. Common ones are HTTP/1.0 and HTTP/1.1.
  • Status code: A 3-digit code indicating the status of the web server's Hypertext Transfer Protocol response. The first number represents the type of current response. For example, 1xx indicates that the request has been received by the server and needs to continue processing.
  • Status code description: Also called status description, it is an explanation of the status code.
HTTP common response codes

We often hear developers say that numbers such as 200, 404, 500, etc. are actually status codes.

HTTP Status Code consists of three decimal digits. The first number of the status code defines the category of the status code. There are five categories of HTTP status codes, as follows:

  • 1xx: Information. Indicates that the server has received the request and needs to continue processing.
  • 2xx: Success. The request has been successfully received and processed.
  • 3xx: Redirect. Further action by the client is required to complete the request.
  • 4xx: Client error. The request sent by the client has a syntax error or the request cannot be fulfilled.
  • 5xx: Server error. An error or abnormal state occurred while the server was processing the request. It may also be that the server realized that it could not complete the processing of the request with the current software and hardware resources.

Here are some common status codes:

status code English name describe
100 Continue The client should continue sending requests
101 Switching Protocols The server switches protocols based on the client's request
200 OK The request was successful. The response headers or data body expected by the request will be returned with this response
203 Accepted The server has accepted the request but has not completed the processing
204 No Content The server processed successfully, but no content was returned
301 Moved Permanently The requested resource has been permanently moved to the new URI, and the browser will automatically be directed to the new URI.
305 Use Proxy The requested resource must be accessed through a proxy
400 Bad Request The syntax of the client request is incorrect and the server cannot understand it.
401 Unauthorized The request requires user authentication
403 Forbidden The server understands the request but refuses to execute it
404 Not Found The request failed, the requested resource cannot be found on the server
500 Internal Server Error Internal server error, unable to complete the request
503 Service Unavailable The server is temporarily unable to process the client's request due to overload or system maintenance.
505 HTTP Version not supported The server does not support, or refuses to support, the version of the HTTP protocol used in the request

 5. Response header

Response headers are used to indicate some additional information that the client needs to use. Similar to the request header, it contains several attributes, one pair per line, conveying fixed information. The format is: attribute name: attribute value. Here are some response header contents.

response header illustrate Example
Server Server application software name and version Server: Tengine
Date The date and time this message was sent Date: Tue, 15 Jun 2021 11:28:29 GMT
Content-Type The MIME type of the current document Content-Type: text/html; charset=utf-8
Content-Encoding The encoding used by the current document Content-Encoding: br
Expires If this time is exceeded, the document is considered to have expired. Expires: Fri, 01 Jan 1990 00:00:00 GMT

6. Response body

Function: Data entity returned by the server

Features: There are many types such as pictures, json, xml, html, etc.

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/133418409