What are the best/common RESTful url verbs and actions?

前端 未结 5 1287
误落风尘
误落风尘 2020-11-28 17:48

I\'m trying to find some info on the best and most common RESTful url actions.

for example, what url do you use for displaying the details of an item, for editing th

5条回答
  •  自闭症患者
    2020-11-28 18:06

    Here is a mapping of your current URLs using the REST principle:

    /question/show/
    

    If you identify the question as a resource, then it should have a unique URL. Using GET to display it (retrieve it) is the common practice. It becomes:

    GET /question/
    

    /question/edit/
    

    Now you want your user to have another view of the same resource that allows him to edit the resource (maybe with form controls).

    Two options here, your application is an application (not a website), then you may be better using JavaScript to transform the resource into an editable resource ono the client side.

    If this is a website, then you can use the same URL with additional information to specify another view, the common practice seems to be:

    GET /question/;edit
    

    /question/update/ (this is the post back url)
    

    This is to change the question, so PUT is the correct method to use:

    PUT /question/
    

    /question/list   (lists the questions)
    

    The list of question is actually the parent resource of a question, so it naturally is:

    GET /question
    

    Now you may need some more:

    POST /question (create a new question and returns its URL)
    DELETE /question/ (deletes a question if this is relevant)
    

    Tada :)

提交回复
热议问题