In a RESTful application, how do we differentiate between an “action” and an HTTP verb (GET, POST, PUT, DELETE)?

前端 未结 2 428
温柔的废话
温柔的废话 2021-01-06 06:56

In a RESTful application, how do we differentiate between an \"action\" and an HTTP verb (GET, POST, PUT, DELETE)?

<
2条回答
  •  忘掉有多难
    2021-01-06 07:24

    here is example like it Rails does

    REST request path    |  action name | Description
    ---------------------|-------------------|-------------
    GET    /profile/new  | new               | Render a form for creating the profile
    POST   /profile      | create            | Create a the profile from the received data
    GET    /profile      | show              | Render a the profile
    GET    /profile/edit | edit              | Render a form for editing the profile
    PUT    /profile      | update            | Update the profile based on the received data
    DELETE /profile      | destroy           | Destroy the profile
    

    I don't see any conflict , Idea is that urls are human readable and you can introduce new URIs to show different representations of the same resource. (like profile/1/edit and profile/1) /profile/new - address of profile that is empty (like profile/1, profile/2 .. etc in show method) But if you want you can suggest that profile/1/edit is some kind of different - nested resource of profile/1/ resource, but I like to thing that it's just other representation of profile/1/ resource =)

    Also is good idea to use plural and singular URI when you working with many resources or with one, example

    /profile/1.html - gives you 1 resource
    /profiles.html - gives you list of profiles
    

提交回复
热议问题