Including Id in URI for PUT requests

前端 未结 4 1551
庸人自扰
庸人自扰 2020-12-24 12:14

I was reading some documents about the appropriate use of URI\'s using rest services and I came across an example for basic GET .. DELETE requests.

The example uri\'

4条回答
  •  感动是毒
    2020-12-24 12:50

    Another benefit for including id in URL for put request is CREATE/UPDATE function can share some logic.

    Let's say you have this user table with 3 colums.

    id(primary key, auto increment) | name | age
    

    Creating a user will generate a ID.

    POST /users
    {
      name: "foo"
      age: 25
    }
    -> generate id 123
    

    Because id is in URL, you can use the same request body structure for updating a user.

    PUT /users/123
    {
      name: "bar"
      age: 25
    }
    

    This might be not so related to REST principal, but it could simplify the code for request handling in practice depending on your languages/frameworks.

提交回复
热议问题