1
头图

Recently, I am doing a series of front-end interview questions. Interested friends can add attention, welcome to correct me and communicate.

Strive to summarize more of each knowledge point, at least to be able to talk about each knowledge point during the interview, so as not to misfire.

HTTP 请求方法

Preface

In daily development, when the front-end and server-end data exchange, the most used is probably the HTTP request. Today we will summarize all the HTTP request methods and understand the meaning of some common status codes returned by the background.

Summary of request method classification

According to the HTTP standard, HTTP requests can use multiple request methods.

HTTP1.0 defines three request methods: GET, POST and HEAD methods.

HTTP1.1 adds six new request methods: OPTIONS, PUT, PATCH, DELETE, TRACE and CONNECT methods.

HTTP 请求方法总结

GET method

GET is the most commonly used HTTP request method. It displays the resource specified in the request and returns the response body. Generally, the expectation for it is safe and idempotent.

The so-called security means that the operation is used to obtain information rather than modify information. In other words, GET requests should generally not have side effects. In other words, it only obtains resource information, just like a database query, it will not modify or add data, and will not affect the state of the resource.

The meaning of security here only refers to non-modified information.

To put it simply, the concept of idempotence means that multiple requests to the same URL should return the same result.

The query string (name/value pair) is sent in the URL of the GET request. Add ? after the URL to connect the query string. Multiple query strings & , such as:

https://cn.bing.com/search?q=%E7%BC%96%E7%A8%8B%E4%B8%89%E6%98%A7&PC=U316&FORM=CHROMN

Some other characteristics of GET requests:

  • GET requests can be cached
  • GET requests are kept in browser history
  • GET request can be bookmarked
  • GET requests should not be used when processing sensitive data
  • GET request has a length limit
  • GET request should only be used to retrieve data (no modification)

HEAD method

Like the GET method, it sends a request for a specified resource to the server, except that the server will not return the text part of the resource, only the header message.

Its advantage is that using this method can obtain "information about the resource" (meta-information or metadata) without having to transmit all the content, and check the header of the resource, such as:

  • If GET /users returns a list of users,
  • Then HEAD /users will make the same request, but will not return the list of users.

Usage scenarios of the HEAD method

  • Under the circumstance of not acquiring resources, understand some information about resources, such as resource types;
  • By checking the status code in the response, you can determine whether the resource exists;
  • By looking at the header, test whether the resource has been modified.

POST method

The POST method is used to submit data to the specified resource and request the server for processing (such as submitting a form or uploading a file), and the data is included in the request text.

POST requests may create new resources or modify existing resources, or both. Each time it is submitted, the data of the form is encoded into the body of the HTTP request by the browser.

The main format of the body of the POST request sent by the browser

  • application/x-www-form-urlencoded used to transmit simple data, such as "key1=value1&key2=value2" format.
  • multipart/form-data mainly used to transfer file content.
  • application/json tells the server that the message body is a serialized JSON string.
  • text/plain plain text format

Multipart/form-data is used because the encoding method of application/x-www-form-urlencoded is very inefficient for binary data such as files.

In addition to the native content-type, developers can also completely customize the data submission format!

Other features of POST request:

  • POST requests will not be cached
  • POST requests will not remain in the browser history
  • POST cannot be bookmarked
  • POST request does not require data length

PUT method

The PUT method is used to send data to the server to create/update resources.

The difference between the PUT and POST methods is that the PUT method is idempotent: calling once is equivalent to calling multiple times in succession (that is, there is no side effect), and calling the POST method multiple times may have side effects, such as resubmitting an order repeatedly.

Possible responses of the PUT method

  • If the target resource does not exist and the PUT method successfully creates a copy, the source server must return 201 ( Created ) to notify the client that the resource has been created.
  • If the target resource already exists and is successfully updated according to the manifestation encapsulated in the request, the source server must return 200 ( OK ) or 204 ( No Content ) to indicate the successful completion of the request.

DELETE method

The DELETE method is to request the server to delete the resource corresponding to the specified URL. However, the client cannot guarantee that the delete operation will be executed because the HTTP specification allows the server to withdraw the request without notifying the client.

Possible response codes for DELETE method

If the DELETE method is executed successfully, there may be the following status codes:

  • The status code 202 (Accepted) indicates that the requested operation may be executed successfully, but execution has not yet begun.
  • The status code 204 (No Content) indicates that the operation has been performed, but there is no further relevant information.
  • The status code 200 (OK) indicates that the operation has been performed, and the description of the related status is provided in the response.

TRACE method

The TRACE method implements a "loop-back" test of messages along the path leading to the target resource, and provides a practical debug mechanism.

The final receiver of the request should reflect the message it receives as it is, and return it to the client as the body of a 200 (OK) response with a Content-Type of message/http.

The final recipient refers to the origin server, or the first server to receive a request with a Max-Forwards value of 0.

We all know that when a client initiates a request, the request may pass through a firewall, proxy, gateway, or some other application. Each node in the middle may modify the original HTTP request. Due to a "loopback" diagnosis, when the request finally reaches the server, the server will bounce back a TRACE response and carry the final appearance of the original request message it received in the response body. In this way, the client can check whether the HTTP request message has been modified while it is being sent.

PATCH method

In the HTTP protocol, the request method PATCH is used to partially modify resources.

In the HTTP protocol, the PUT method has been used to indicate the overall coverage of resources, while the POST method does not provide support for the standard patch format. Unlike the PUT method, and similar to the POST method, the PATCH method is non-idempotent, which means that multiple consecutive identical requests will have different effects.

To determine whether a server supports the PATCH method, it depends on whether it is added to the response header Allow or Access-Control-Allow-Methods (in the case of cross-domain access, CORS) method list.

Another implicit sign of supporting the PATCH method is the appearance of the Accept-Patch header, which clarifies the format of the patch file that can be accepted by the server.

response

The 204 status code indicates that this is a successful response because the response does not contain the message body.

OPTIONS method

The OPTIONS method is used to obtain the communication options supported by the target resource.

The client can use the OPTIONS method for a specific URL, or it can be used for the entire site (by setting the URL to "*").

If the request is successful, it will include a header named "Allow" in the HTTP header, and the value is the supported method, such as "GET, POST".

Example of use

You can use the OPTIONS method to initiate a request to the server to detect which HTTP methods the server supports. The response message contains an Allow header field. The value of this field indicates all HTTP methods supported by the server:

HTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Date: Thu, 13 Oct 2016 11:45:00 GMT
Expires: Thu, 20 Oct 2016 11:45:00 GMT
Server: EOS (lax004/2813)
x-ec-custom-error: 1
Content-Length: 0

CONNECT method

The CONNECT method can open a two-way communication channel between the client and the requested resource. It can be used to create tunnels.

Summarize

The above is the summary of the content of the HTTP method. Using each method reasonably according to the scenario can optimize performance and increase network security.

~

~ End of this article, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, and create interesting souls!

Hello, everyone, I am the author Programming Samadhi Hermit King , my is "161101bac40b9a Programming Samadhi ", welcome to pay attention, I hope you can give me your advice!

Come, with expectations, I have Moxiang to greet you! You return, no matter the gains or losses, only with the lingering rhyme as a gift!

Pay equal attention to knowledge and skills, both internal and external skills, both theory and practice must be grasped, and both hands must be hard!


编程三昧
54 声望10 粉丝

学习有趣的知识,交识有趣的朋友,造就有趣的灵魂!