The RESTful flow?

前端 未结 8 1255
傲寒
傲寒 2021-01-16 06:34

So...

I\'ve been reading about REST a little bit, and the idea behind it sounds nice, but the question is, can it be easily integrated into the standard flow of a we

相关标签:
8条回答
  • 2021-01-16 07:14

    If you really have no choice about using the DELETE verb then I would suggest something like the following:

    POST http://mysite.com/Trashcan?resourceUrl=/Customer/75
    

    What url you use really does not matter to REST, however, it is easier to understand the REST way of interacting if your urls avoid verbs completely.

    I have seen so many questions from both Rails and ASP.NET MVC users who need to go beyond the standard "actions" and it is so tempting to just add a new action on the controller. The problem with doing this is that you just threw away the uniform interface constraint of REST.

    The trashcan metaphor is not the only way of doing deletes restfully but I would argue that it is just as clear to read as putting a "delete" in the url.

    Here are some more "noun-based" ways of replacing verbs.

    POST http://mysite.com/Printer/75/PrintQueue?url=http://mysite.com/Document/xyz
    POST http://mysite.com/CurrentLogins?user=bob
    POST http://mysite.com/QueryProcessor?query=FindMyInformation
    POST http://mysite.com/SearchEngine?searchTerms=cat,blue,furry
    POST http://mysite.com/OrderProcessor?cart=http://mysite.com/user/2323/cart
    

    Sometimes you have to think out of the box a little to come up with a noun based url, and may seem pedantic to try and do this, but for me the benefit comes from the ability to manage my variables. I am going to have a variable number of resources in my interface, no matter what I do, if I can fix the number of verbs that can operate on those resources then I reduce one of my variables.

    0 讨论(0)
  • 2021-01-16 07:18

    I don't think REST is rarely used. You're using it right now, on StackOverflow. As far as your specific example goes, you can send DELETE requests though XMLHttpRequest in browsers that support it. When JS is off, or for non-compliant browsers, you can do something like:

    POST http://foo.com/delete?post=5

    Not ideal, but still more restful than many sites.

    EDIT: Changed to POST

    0 讨论(0)
提交回复
热议问题