Plain language explanation of REST and RESTful

Original link: https://www.zhihu.com/question/28557115/answer/318513169

http protocol is currently the most used on the Internet, no one.

But the founder of http have always felt that in the past 10 years, the people are all wrong use Http.

 

How do you say this sentence?

If you want to delete the data, past practice is usually delete / {id}

If you want to update a data, the data may be put Post Body, and the method is update / {id}, or artichle / {id}? Method = update

 

This approach allows Roy Fielding is very irritable, he felt that the world should not be like this, all the people are in serious misunderstandings and wrong misconception Http is designed, like the invention of gunpowder but only use it for fireworks.

 

So what is the correct way to use it? Rest if you look at the various features, you probably really hard to understand Rest, but if you look at people mistakenly use http children what they going wrong, what's Rest is particularly easy to understand.

 

Seven deadly sins

The first confusion.

Ten thousand individual mind has millions of naming Url, Url Uniform Resource Locator, is the focus of resources. Many people think of it as a one size fits all, each individual virtual pages are free to use, the various operations can be superimposed.

This is one source of confusion.

 

Second, greed.

Stateful and stateless all mixed together. Especially in the cart or an application log, it is often to refresh the user experience is lost simply Bang Bang clatter. Each individual request and not respond to some features, many features in mixed together.

 

This is a greedy human nature, but also a variety Hack origin, as long as they get rid of the problem, there is always someone he considered the most convenient way to solve the problem, such as car door handles broken when the direct line rope handles, emmmm this really great ah.

 

 

Article disorder.

The results returned are often very casual, various error messages with status code Http originally constituted, but many people still prefer the error message is returned in the return value. The most common is the Code and Message, of course, for this, I personally reserved doubt, in my opinion, an internal error Http own errors and the need for separate server or in constant level, can not be mixed together. But not so great in the eyes of God, then this discussion.

 

 

Well, I made do not go.

So how to solve these problems? Gospel OCD is awarded to the rules, the first rule is clear what Url is how to use.

Url is all essence, it should be a resource. An independent Url address, that corresponds to a unique resource.

how about it? This feeling is not the Bang Bang da? An ice cream, a teacher, a house in the Url is a corresponding resource, there will be no extra Url correspondence with him, would not indicate there are multiple Url address ~~ note that this point is the Url address, not a single argument, he is a / room / {room_id} such a thing, give chestnuts, / room / 3242 this means that the room number 3242.

 

This is a refreshing world ah, you think, before Url is what should I open room, may be / open / room / 3242 I would probably check out / exit / 3242 / room, I want to take care of the room, may be room / 3242? method = clean.

 

enough! The whole mess is enough to make the world a refreshing return to nature, a room is / room / 3242 Url address of nothing else.

 

I want to have this resource on how to do the operation?

This is the Bang Bang da great God to come out, and there are several http Method was it?

get, put, post, delete, there are other hidden four kinds.

In the past chaotic world, often it is the Get and Post. If not for the Get does not support large data transfers, I think even the Post will be someone to use. (Imagine Roy Fielding in anger shouting at the computer screen, Http Method A total of eight, and you have just caught Get a hair sheep wool pull out pull out pull out pull out pull out).

 

And what to do with the resources of the most common is? CRUD, right, is to create, read, update, delete. Look at Http's Method? It is not perfect? In fact, the beginning of a strange Fielding Father named inaccurate, if that is the beginning of the Get method is called Read, Put method is called Update, Post called the Create this nice. . .

 

You use a Get, it also found that the so-called no restrictions nothing, and very difficult to understand the difference Put and Post, the law can not prohibit to ah, uh, do not stare at me Father, I nonsense.

 

Together, these four methods you enough waves? You have to find itself out of operation for more resources to ah, I have not used it four Method. If these four really not enough, what is the problem, big deal I re-change http protocol ah.

 

In fact, simply for the Rest understand here is enough. Follow things that are at the basis of this one to come out of the sky, more than obsessive-compulsive disorder OCD, of course, I'm a hundred percent no state support.

Above all kinds of statements may not be accurate, it is purely my trail information and a variety of obscenity, not textual, but a matter of conscience, I could not understand the long dark era of Url naming style, so At that time the first contact with the Rest of the time, and instantly find true love, I rely on, this is not what I have been trying to answer?

 

But I always wanted only naming conventions, never put their own thinking on the perspective of a url is a resource, the point of view of all changes to the resources of all operations ah.

So you can understand to the extent that more is that you have to figure out what the problem you want to solve, if your problem is only understood Rest, I'm afraid you quite understand, if your problem is how to solve the problem Url confusion, you instead soon be able to understand the ~

 

 

Finally, I hope you remember this better way of thinking and learning problems, and more with this mentality to understand and Rest Restful, write an article to more rigorous ~~

Reprinted from: https://www.zhihu.com/question/28557115/answer/318513169

Guess you like

Origin blog.csdn.net/qq_37896194/article/details/97920339