简体   繁体   English

在基于JSON的REST API上进行PUT的最常见预期行为是什么? 文档替换或部分更新)?

[英]What's the most common expected behavior for a PUT on a JSON based REST API? A document replacement or a partial update)?

I know there's a whole RFC for HTTP methods but I was wondering what would be the expected behaviour from a PUT to a REST API that responds with Content-type: application/json and accepts JSON encoded bodies. 我知道HTTP方法有一个完整的RFC,但我想知道从PUT到REST API的预期行为是什么,该API会响应Content-type: application/json并接受JSON编码的主体。

  • Is it expected to replace the document entirely with the new JSON object passed in the body? 是否期望用正文中传递的新JSON对象完全替换文档?
  • Is it expected to modify only the attributes passed in the JSON body? 是否期望仅修改JSON正文中传递的属性? (as its the recommended behaviour for the PATCH method RFC 5789 ). (作为PATCH方法RFC 5789的建议行为)。
  • What if the documents that the API exposes do not conform to an schema and the JSON object in the body has new properties not present in the current document?. 如果API公开的文档不符合架构,并且主体中的JSON对象具有当前文档中不存在的新属性,该怎么办? Should it add them? 应该添加它们吗?

Any comment or resource for me to read will be welcome . 任何供我阅读的评论或资源都将受到欢迎

1) and 2) PUT means replace. 1)和2)PUT表示更换。 Using it for partial replacement is incorrect. 使用它进行部分替换是不正确的。 That's what PATCH is for. 那就是PATCH的目的。

3) That's up to your application logic. 3)这取决于您的应用程序逻辑。

In practice, either of those approaches is all right as long you stick to it throughout the whole application so you keep consistency. 实际上,只要在整个应用程序中坚持使用这些方法,那么这两种方法中的任何一种都可以,这样可以保持一致性。 I personally like to use PUT if I want to update all the attributes of a record according to the ID. 如果要根据ID更新记录的所有属性,我个人喜欢使用PUT。 This way I can save the PATCH method for endpoints where I need to specify and update only some attributes like the a typical change password request where I only need to update a specific attribute. 这样,我可以将PATCH方法保存在仅需要指定和更新一些属性的端点上,例如典型的更改密码请求,在该端点上我只需要更新特定的属性即可。

I really recommend this book: http://www.amazon.com/REST-API-Design-Handbook-ebook/dp/B00890OBFI/ref=sr_1_2?s=books&ie=UTF8&qid=1425669926&sr=1-2&keywords=rest+api 我真的推荐这本书: http : //www.amazon.com/REST-API-Design-Handbook-ebook/dp/B00890OBFI/ref=sr_1_2?s= books& ie= UTF8& qid= 1425669926& sr= 1-2& keywords=rest+api

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM