This is the third day of my participation in the August Text Challenge.More challenges in August

Recently I am doing the front end test summary series, interested friends can add attention, welcome to correct, exchange.

Strive for each knowledge point can sum up some more, at least to do in the interview, for each knowledge point can kan up, not dumb fire.

preface

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

Request method classification summary

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

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

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

The GET method

GET, the most common HTTP request method, displays the resource specified by the request and returns the response body, which is expected to be safe and idempotent.

Security means that the operation is used to retrieve information, not modify it. In other words, GET requests should generally have no side effects. That is, it simply retrieves resource information, just like a database query, without modifying or adding data or affecting the state of the resource.

Security here refers only to unmodified information.

The idea of idempotent, put simply, is 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, followed by? Concatenate the query string. Multiple query strings are concatenated by &, for example:

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

Some other features of GET requests:

  • GET requests can be cached
  • GET requests remain in browser history
  • GET requests can be bookmarked
  • GET requests should not be used when working with sensitive data
  • GET requests have a length limit
  • GET requests should only be used to retrieve data (without modification)

The HEAD method

As with the GET method, a request is made to the server for a specified resource, except that the server will not return the article portion of the resource, only the header message.

The advantage of this method is that “information about the resource” (meta information or metadata) can be retrieved to check the header of the resource without having to transmit the entire content, for example:

  • 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 for the HEAD method

  • Learn some information about resources, such as resource types, without obtaining resources.
  • By viewing the status code in the response, you can determine whether the resource exists.
  • Test whether the resource has been modified by looking at the header.

POST method

The POST method is used to submit data to a specified resource and request the server to process it (for example, submit a form or upload a file). The data is included in the request text.

A POST request may create a new resource or modify an existing resource, or both. Each time it is submitted, the form’s data is encoded by the browser into the BODY of the HTTP request.

The main format for the body of a POST request made by the browser

  • Application/X-www-form-urlencoded is used to transmit simple data such as “key1=value1&key2=value2”.
  • Multipart /form-data is used to transfer file content.
  • Application/JSON tells the server that the message body is a serialized JSON string.
  • Text /plain The format is plain text

Multipart /form-data is used because application/ X-www-form-urlencoded encoding is very inefficient for binary data like files.

In addition to the native Content-Type, developers can also fully customize the data submission format!

Other features of POST requests:

  • POST requests are not cached
  • POST requests do not remain in browser history
  • POST cannot be bookmarked
  • POST requests have no data length requirements

PUT method

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

The difference between PUT and POST is that the PUT method is idempotent: calling it once is equivalent to calling it multiple times in a row (that is, without side effects), while calling it multiple times in a row can have side effects, such as repeating an order multiple times.

Possible responses of the PUT method

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

The DELETE method

The DELETE method asks the server to DELETE the resource corresponding to the specified URL. However, the client cannot guarantee that the deletion will be performed because the HTTP specification allows the server to revoke the request without notifying the client.

The possible response code of the DELETE method

If the DELETE method executes successfully, the following status codes may exist:

  • Status code 202 (Accepted) indicates that the requested operation is likely to execute successfully, but has not yet begun.
  • Status code 204 (No Content) Indicates that the operation has been performed, but No further information is available.
  • Status code 200 (OK) indicates that the operation has been performed and the response provides a description of the status.

The TRACE method

The TRACE method provides a useful debugging mechanism for “loop-back” testing of messages along the path to the target resource.

The ultimate recipient of the request should reflect the message it received as the body of a 200 (OK) response with a Content-type message/ HTTP.

The final receiver is the origin server or the first server to receive the request for max-forwards with a value of 0.

As we all know, when a client initiates a request, the request may pass through a firewall, proxy, gateway, or some other application. Each of these nodes may modify the original HTTP request. Because of a “loopback” diagnosis, when the request finally reaches the server, the server bounces back a TRACE response and carries in the response body the final look of the original request message it received. In this way, the client can check whether HTTP request packets are modified on the way to 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 represent the overall coverage of the resource, while the POST method does not support the standard patch format. Unlike the PUT method, and like the POST method, the PATCH method is non-idempotent, which means that multiple consecutive requests of the same type have different effects.

To determine whether a server supports the PATCH method, see if it has added it to the list of Methods allowed or Access-Control-Allow-Methods (CORS, for cross-domain Access) on the response header.

Another implicit indication of support for the PATCH method is the appearance of the accept-patch header, which specifies the format of the PATCH file that the server can Accept.

The response

204 The status code indicates that this is a successful response because there is no message body in the response.

The OPTIONS method

The OPTIONS method is used to get the communication OPTIONS supported by the destination resource.

The client can use the OPTIONS method for a specific URL or for the entire site (by setting the URL to “*”).

If the request is successful, it includes a header named “Allow” in the HTTP header with the value of the supported method, such as “GET, POST.”

Use the sample

You can use the OPTIONS method to make a request to the server to check which HTTP methods the server supports. The response packet contains a Allow header field, and 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
Copy the code

The CONNECT method

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

conclusion

The above is the summary of HTTP methods. Using each method properly can optimize performance and increase network security.

~

Thanks for reading!

~

Learn interesting knowledge, meet interesting friends, shape interesting soul!

Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!

You come, with expectations, I have ink to welcome! You return, no matter gain or loss, only to yu Yun give each other!

Knowledge and skills should be paid equal attention to, internal force and external power should be repaired simultaneously, theory and practice should grasp both hands, both hands should be hard!