What is restful? What are the rest request methods and what are the differences?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[what is restful? What are the rest request methods and what are the differences?]

Hello, I'm the 3rd student of Shanghai Branch of it Academy. I'm an honest and kind java programmer. Today, I'd like to share with you a knowledge point in the Java task of the Academy: what is restful? What are the rest request methods and what are the differences?

Restful is a network API interface that conforms to the rest architecture style. It fully recognizes that HTTP is used to identify resources. Restful URLs are resource oriented and can uniquely identify and locate resources. What to do with the resource identified by the URL is determined by the HTTP method. There are four rest request methods, including get, post, put and delete Get resources, add resources, update resources and delete resources respectively

How to write restful that conforms to the rest architecture style?

1. Verbs are not allowed in the URL

2. URL can uniquely identify a single resource or a class of resources

3. Use HTTP verbs to operate the resources identified by the URL (get, PST, put, update, delete))

Example of incorrect writing

Wrong writing

Verbs are used in the URL of the network API interface, which does not only identify and locate resources

Examples of correct writing

Correct writing

The network API interface URL only identifies and locates resources. The HTTP method post indicates that it is a processor that adds resources

Rest request method parsing

GET

In rest, the get method is mainly used to obtain resources. It can send parameters, but there are restrictions, and the parameters will be marked with "get"? The beginning form is attached to the end of the URL

Get parameter

Because the parameter information will be completely exposed, it is not recommended to send important information. The get method generates a TCP datagram and sends it at one time.

The canonical get method processor should be idempotent, that is, no matter how many get requests are sent to a resource, the data will not be changed or damaged.

Idempotent should be guaranteed when writing processors to improve security

POST

The post method is mainly used to add resources in the rest request. The parameter information is stored in the message body of the request message, which is relatively safe and can send large information

Because the parameter information of post is relatively safe in the message body of the party, it is generally used to send important data, and post generates two TCP datagrams, which need to be sent twice

For the essential difference between get and post, the blog is very good

The normalized post method processor is not idempotent. Therefore, if users post a resource repeatedly, they should limit and process it in the processor to ensure that the data will not be damaged or changed, so as to improve security

PUT

The put method is mainly used to update resources in rest, because most browsers do not support put and delete, and will automatically convert put and delete requests into get and post Therefore, in order to use the put and delete methods, you need to send the request as post and use the hidden field in the form to send the real request.

The parameters of the put method are stored in the message like post. It is also secure and can send large information.

The put method is idempotent. Any put request for the same data made to the same URL resource will change the data consistently. For example, update the name value of / student / 2 to bobdylan

No matter how many times the request is submitted, the name value of the / student / 2 resource will be the same as submitting a request

DELETE

Delete is mainly used to delete resources in rest requests, because most browsers do not support put and delete, and will automatically convert put and delete requests into get and post Therefore, in order to use the put and delete methods, you need to send the request as post and use the hidden field in the form to send the real request.

The parameters of the delete method are stored in the message body like post, which is secure and can send large information. The delete method is idempotent and will not destroy the data no matter how many delete requests are made to the same resource

common problem

The browser automatically converts put and delete to get and post. If the container cannot find the corresponding processor, an error is reported

The rest style specifies that the URL identifies the resource and uses the four methods of HTTP to operate the resource. However, when the browser sends a request, it will automatically convert put and post into get and post In this way, the rest style becomes a chicken rib, and sending a request will report an error that the get method or post method cannot be found.

The reason why delete and put methods are not supported is that html4 officially only supports get and post methods in forms, ignoring put, delete and other HTTP methods Although all HTTP methods are supported in HTML5 and some new browsers, it is impossible for all users to use the latest browser.

Solution

1. First, the front-end personnel send it through Ajax. Because they don't understand the front-end, they don't elaborate

2. Configure the filter on the server side by using the hidden field in the form to send the real request

3. Use spring's SF: form form to submit

The first is not detailed

The second is demonstrated in coding practice

The third method is seen in the book of spring actual combat because of incomplete data. My own attempt to implement it failed. See page 305 of spring's actual combat book

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It academy "we believe that everyone can become an engineer. From now on, find a senior brother to get you started, control your learning rhythm, and stop being confused on the way to learning".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me ~ my invitation code is 16318412, or you can click this link directly: http://www.jnshu.com/login/1/16318412

Author: bury your head and go forward to find my own link: https://www.jianshu.com/p/dfe3077ddbcd Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: a gathering place for beginners to switch to the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>