我有一个 REST URI 的设计问题。
假设我有一个汽车资源(在数据库中映射了一些属性)。 如果我想用另一辆车的信息更新汽车资源,我知道我可以调用
PUT /base_url/api/cars/1
I update the car with id == 1 with the informations in the request body
但是如果我想用 id == 2 的汽车信息更新 id == 1 的汽车呢? (我只想传递 id,因为副本是由服务器内部处理的) 我如何在休息时设计这种类型的请求?
最佳答案
您应该使用 PATCH 来代替。
The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version. The PATCH method affects the resource identified by the Request-URI, and it also MAY have side effects on other resources; i.e., new resources may be created, or existing ones modified, by the application of a PATCH.
像这样的事情就可以了:
PATCH /base_url/api/cars/1
{template: {id: 2}}
不要在查询中发送 ID,因为它是 URI 的一部分,而 URI 是目标资源的标识符。
关于web-services - REST URI 设计 : how update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315274/