用于替换组成员的 REST 端点
REST endpoint to replace a member of a group
有多个集合,每个集合都可以通过唯一的 ID 来标识。每个集合中都有多个人,每个人都可以通过他们的员工 ID 来识别。很显然,一个人不能在同一个集合中两次。
我需要用给定集合中的新人物替换给定人物。
是否应该使用两个端点来完成:
DELETE /collections/123/employees/321
POST /collections/123 {"employeeId": 222}
或者可以用一个来完成吗:
PUT /collections/123/employees/321 {"employeeId": 222}
PUT 应该是 idempotent:
An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state. In other words, an idempotent method should not have any side-effects (except for keeping statistics). Implemented correctly, the GET, HEAD, PUT, and DELETE method are idempotent, but not the POST method.
如果您调用 back-end 会发生什么:
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
如果321记录不存在了,应该如何处理?如果新的employeeId已经存在,应该如何处理?
从逻辑上讲,对我来说,将它们保留为两个单独的调用(删除旧成员并添加新成员)会更有意义。虽然您可以 在 PUT 请求中处理此问题,同时保持请求的幂等性,但潜在用户并不清楚此调用的预期功能是什么。
有多个集合,每个集合都可以通过唯一的 ID 来标识。每个集合中都有多个人,每个人都可以通过他们的员工 ID 来识别。很显然,一个人不能在同一个集合中两次。
我需要用给定集合中的新人物替换给定人物。
是否应该使用两个端点来完成:
DELETE /collections/123/employees/321
POST /collections/123 {"employeeId": 222}
或者可以用一个来完成吗:
PUT /collections/123/employees/321 {"employeeId": 222}
PUT 应该是 idempotent:
An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state. In other words, an idempotent method should not have any side-effects (except for keeping statistics). Implemented correctly, the GET, HEAD, PUT, and DELETE method are idempotent, but not the POST method.
如果您调用 back-end 会发生什么:
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
PUT /collections/123/employees/321 {"employeeId": 222}
如果321记录不存在了,应该如何处理?如果新的employeeId已经存在,应该如何处理?
从逻辑上讲,对我来说,将它们保留为两个单独的调用(删除旧成员并添加新成员)会更有意义。虽然您可以 在 PUT 请求中处理此问题,同时保持请求的幂等性,但潜在用户并不清楚此调用的预期功能是什么。