REST api PUT 是否应该接受带有单个已编辑 属性 和文档 ID 的请求?
Should REST api PUT accept a request with a single edited property and the id of the document?
或者客户端是否应该每次 属性 发送一次,即使它们没有被编辑?
想象一下我在服务器上的数据库中存储了以下文档:
{
_id: "1",
name: "Mario",
surname: "Rossi"
}
现在假设客户只想更新名称。这个 PUT 请求应该被接受吗?
{
_id: "1",
name: "Giorgio"
}
或者客户应该发送所有属性?
{
_id: "1",
name: "Giorgio",
surname: "Rossi"
}
似乎部分编辑可以节省一些带宽,发送额外数据确实没有意义,但我不确定这里的标准是什么。
谢谢!
您通过 PUT
请求发送的表示应该与您希望从 GET
请求返回的表示相同。
A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response. -- RFC 7231
换句话说,包含在 PUT 请求 body 中的表示应该是完整的表示。
另见HTTP Patch的介绍:
The existing HTTP PUT method only allows a complete replacement of a document.
Roy Fielding,2012 年写作:
When a server that implemented PUT prior to the introduction of Content-Range received a partial content body that included such a range, they would replace the entire resource representation with the partial body. That is how PUT was defined to work.... the introduction of partial PUT would only be possible with a strong coupling between client and origin server implementations, which violates the design of the HTTP.
有些情况下表示非常大(比 HTTP headers 大得多),而我们所做的更改很小,发送更改的表示是明智的,而不是比完整的表示。
但是 PUT
不是我们在那种情况下使用的方法,因为语义是错误的。我们这里符合标准的选择是用 PUT
结束整个(修订的)表示,或者用 PATCH
.
表示 patch-document 中的变化。
或者客户端是否应该每次 属性 发送一次,即使它们没有被编辑?
想象一下我在服务器上的数据库中存储了以下文档:
{
_id: "1",
name: "Mario",
surname: "Rossi"
}
现在假设客户只想更新名称。这个 PUT 请求应该被接受吗?
{
_id: "1",
name: "Giorgio"
}
或者客户应该发送所有属性?
{
_id: "1",
name: "Giorgio",
surname: "Rossi"
}
似乎部分编辑可以节省一些带宽,发送额外数据确实没有意义,但我不确定这里的标准是什么。 谢谢!
您通过 PUT
请求发送的表示应该与您希望从 GET
请求返回的表示相同。
A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response. -- RFC 7231
换句话说,包含在 PUT 请求 body 中的表示应该是完整的表示。
另见HTTP Patch的介绍:
The existing HTTP PUT method only allows a complete replacement of a document.
Roy Fielding,2012 年写作:
When a server that implemented PUT prior to the introduction of Content-Range received a partial content body that included such a range, they would replace the entire resource representation with the partial body. That is how PUT was defined to work.... the introduction of partial PUT would only be possible with a strong coupling between client and origin server implementations, which violates the design of the HTTP.
有些情况下表示非常大(比 HTTP headers 大得多),而我们所做的更改很小,发送更改的表示是明智的,而不是比完整的表示。
但是 PUT
不是我们在那种情况下使用的方法,因为语义是错误的。我们这里符合标准的选择是用 PUT
结束整个(修订的)表示,或者用 PATCH
.