web-services - REST URI 设计 : how update

标签 web-services rest http uri

我有一个 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/

相关文章:

java - axis2客户端NTLM身份验证

php - 如何在 Guzzle 中设置引用 header

php - 流膨胀包装器?

c# - 使用 Web 服务接受数据 C# .NET 3.5

java - 在云上部署 Web 服务

javascript - 将项目从 .Net2.0 转换为 .Net4.0 后 WebMethod 调用失败

java - 如何使用 OneDrive SDK for Android 发送搜索请求

java - 没有注释的 Jersey 方法参数

api - HTTP 状态代码 >= 300 返回 JSON

javascript - 多次调用 api 时,AJAX 请求有时会被取消