You can use “PUT” 或 “PATCH” 来编辑次要资源。

huangapple go评论100阅读模式
英文:

With REST, should I use PUT or PATCH to edit a secondary ressource?

问题

To update Bob's car at the route /users/1/car, you should use the PUT method if you want to replace the entire car object with a new one. If you only want to update specific properties of Bob's car, you can use the PATCH method. The choice between PUT and PATCH depends on whether you want to completely replace the car object or make partial updates to it.

英文:

So I have users and each user has a car. A user:

{
  name: 'Bob',
  car: {
    brand: 'Ford'
  }
}

To update Bob's car I could use an endpoint PUT /users/1 and update the entirety of Bob, or use an endpoint PATCH /users/1 and specify in the body that I only want to update Bob's car.

But if I want a route that only refers to Bob's car /users/1/car what method should I use to update the car? PUT, PATCH, or something else?

car is called a secondary resource of the user I believe, but in this case, I do not know what is the entity, is it the user or is it the car? If it's the car, then I believe I should use PUT, but if it is still the user then it should be PATCH.

答案1

得分: 1

With REST, should I use PUT or PATCH to edit a secondary resource?

使用REST,我应该使用PUT还是PATCH来编辑次要资源?


An important constraint in the REST architectural style is the uniform interface constraint. Among other things, this means that request semantics are the same for all target resources.

REST架构风格中的一个重要约束是统一接口约束。 其中之一是指请求语义对于所有目标资源都是相同的。

Therefore: PUT and PATCH messages that target /users/1/car should have the same semantics as PUT and PATCH messages that target /users/1 should have the same semantics as PUT and PATCH message that target /questions/76036040/with-rest-should-i-use-put-or-patch-to-edit-a-secondary-ressource.

因此:针对/users/1/car的PUT和PATCH消息应该具有与针对/users/1的PUT和PATCH消息相同的语义,应该具有与针对/questions/76036040/with-rest-should-i-use-put-or-patch-to-edit-a-secondary-ressource的PUT和PATCH消息相同的语义。

Specifically: we use PUT when the request body is a representation of our desired state for the target resource. We use PATCH when the request body is a patch-document, which is to say a description of a set of changes that should be made to the target resource.

具体来说:当请求正文是目标资源所需状态的表示时,我们使用PUT。当请求正文是补丁文档时,我们使用PATCH,也就是说,它是一组应该对目标资源进行的更改的描述。

in this case, I do not know what is the entity, is it the user or is it the car? If it's the car, then I believe I should use PUT, but if it is still the user then it should be PATCH.

在这种情况下,我不知道实体是什么,是用户还是汽车?如果是汽车,那么我认为我应该使用PUT,但如果仍然是用户,那么应该使用PATCH。

Doesn't matter - PUT and PATCH are about representations of resources, not representations of entities. Our REST API hides our domain entities behind a facade that looks like a web site; the semantics of the messages we are sending are expressed in the abstractions of the web site, not the underlying implementation.

不重要 - PUT和PATCH涉及到资源的表示,而不是实体的表示。我们的REST API将我们的领域实体隐藏在看起来像网站的外观后面;我们发送的消息的语义是通过网站的抽象来表示的,而不是底层实现。

So if my route is /users/1/car and changes the entirety of user 1 car, then it is PUT?

那么如果我的路由是/users/1/car,并且更改了用户1的整个汽车,那么就是PUT吗?

If the target URI is /users/1/car, and the payload is a complete replacement representation of the /users/1/car resource, then the method should be PUT.

如果目标URI是/users/1/car,并且有效负载是/users/1/car资源的完整替代表示,则方法应该是PUT。

If the target URI is /users/1/car, and the payload is a patch-document that describes a set of changes to the representation of the /users/1/car resource, then the method should be PATCH.

如果目标URI是/users/1/car,并且有效负载是描述对/users/1/car资源表示进行的一组更改的补丁文档,则方法应该是PATCH。

The fact that there is also a /users/1 resource which may be impacted by the changes in /users/1/car is not relevant to the question.

/users/1资源也可能会受到/users/1/car中的更改的影响,这个事实与问题无关。

Note that sending a PATCH to /users/1/car with a patch document that describes a set of changes to /users/1 is completely nonsensical (it is misleading to general purpose HTTP components who don't know that the two resources are related to each other).

请注意,向/users/1/car发送一个补丁,其中包含描述对/users/1的一组更改的补丁文档是完全不合逻辑的(对于不知道这两个资源彼此相关的通用HTTP组件来说,这是具有误导性的)。

英文:

> With REST, should I use PUT or PATCH to edit a secondary resource?

Careful - secondary resource can be understood to mean something very different from what you are describing here.


An important constraint in the REST architectural style is the uniform interface constraint. Among other things, this means that request semantics are the same for all target resources.

Therefore: PUT and PATCH messages that target /users/1/car should have the same semantics as PUT and PATCH messages that target /users/1 should have the same semantics as PUT and PATCH message that target /questions/76036040/with-rest-should-i-use-put-or-patch-to-edit-a-secondary-ressource.

Specifically: we use PUT when the request body is a representation of our desired state for the target resource. We use PATCH when the request body is a patch-document, which is to say a description of a set of changes that should be made to the target resource.


> in this case, I do not know what is the entity, is it the user or is it the car? If it's the car, then I believe I should use PUT, but if it is still the user then it should be PATCH.

Doesn't matter - PUT and PATCH are about representations of resources, not representations of entities. Our REST API hides our domain entities behind a facade that looks like a web site; the semantics of the messages we are sending are expressed in the abstractions of the web site, not the underlying implementation.


> So if my route is /users/1/car and changes the entirety of user 1 car, then it is PUT?

If the target URI is /users/1/car, and the payload is a complete replacement representation of the /users/1/car resource, then the method should be PUT.

If the target URI is /users/1/car, and the payload is a patch-document that describes a set of changes to the representation of the /users/1/car resource, then the method should be PATCH.

The fact that there is also a /users/1 resource which may be impacted by the changes in /users/1/car is not relevant to the question.

Note that sending a PATCH to /users/1/car with a patch document that describes a set of changes to /users/1 is completely nonsensical (it is misleading to general purpose HTTP components who don't know that the two resources are related to each other).

huangapple
  • 本文由 发表于 2023年4月17日 22:05:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定