简体   繁体   English

是否应该在REST API PUT请求中传递资源及其相关资源的所有字段?

[英]Should all the fields of a resource and its related resources be passed in a REST API PUT request?

Let's say I have a ticket & comment resource. 假设我有一张票和评论资源。 Tickets can have many comments. 门票可以有很多评论。 In your update endpoint HTTP PUT /api/tickets/<ticket_id>/ , should I require the client to pass all the fields of a ticket + ALL its comments when updating a ticket? 在更新端点HTTP PUT /api/tickets/<ticket_id>/ ,我是否应该要求客户端在更新HTTP PUT /api/tickets/<ticket_id>/所有字段及其所有注释?

I asked some developers and some of them said that I should just pass the fields that will be modified since its lighter, faster in terms of performance and easier to use. 我问了一些开发人员,他们中的一些人说我应该传递将要修改的字段,因为它更轻,性能更快,更容易使用。 And some said that I should pass all the fields of a ticket + ALL its comments since PUT request should be idempotent. 有些人说我应该通过票证的所有字段+所有评论,因为PUT请求应该是幂等的。 But my concern is when there are too many comments, the payload will be very big. 但我担心的是,当评论太多时,有效载荷会非常大。

Yes, since a PUT request should replace the entity-to-update in it's entirety. 是的,因为PUT请求应该替换整个实体到更新。 If you want to do a partial update of an entity, use a PATCH request. 如果要对实体进行部分更新,请使用PATCH请求。

Also see the rfc for reference 另请参阅rfc以供参考

PATCH Method for HTTP HTTP的PATCH方法

Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification. 扩展超文本传输​​协议(HTTP)的几个应用程序需要一个功能来进行部分资源修改。

The existing HTTP PUT method only allows a complete replacement of a document. 现有的HTTP PUT方法仅允许完全替换文档。 This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource. 此提议添加了一个新的HTTP方法PATCH来修改现有的HTTP资源。

So in your situation, it's probably a lot more efficient to use a PATCH request to do a partial update. 因此,在您的情况下,使用PATCH请求进行部分更新可能会更有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 REST-当我访问资源时,它应该包含其所有子资源还是应该在新请求中获取它们? - REST - when I access a resource should it contain all its sub resources or should they be fetched in a new request? 设计一个 RESTful api 用于一次创建资源及其相关资源 - Design a RESTful api for creating a resource and its related resources at once REST api - 客户端应如何为其用户资源提供userid到请求URL? - REST api - How should the client supply userid to the request URL for its user resource? URL的其余API版本与相关资源或所有API相同 - Rest API version in URL same for related resources or all APIs Codeigniter Rest API:放置请求未传递ID - Codeigniter Rest API : Put request doesn't get passed id REST API:是否应该使用PUT或DELETE来部分更新资源? - REST API: Should we use PUT or DELETE to update resource partially? CRUD Rest API 相关资源 - CRUD Rest API related resource 当使用者要求与不存在的资源相关的资源集合时,我应该发送什么API响应? - What API response should I send when a consumer asks for a collection of resources related to a resource that doesn't exist? 如何使用Codeigniter Phil Sturgeon / Chris Kacerguis REST API将资源置于资源之下 - How to put resources under resource with Codeigniter Phil Sturgeon/Chris Kacerguis REST API REST 资源的 JSON 表示是否应该使用相关资源的 URI? - Should JSON representation of REST resources use URIs for related resources?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM