简体   繁体   English

REST API-如何处理PUT方法中的可选参数? 用PATCH方法实现的区别是什么

[英]REST API - How to handle optional parameter in PUT method ? what is difference in implementation with PATCH method

I'm a little confused about put method with optional paramter. 我对带有可选参数的put方法感到有些困惑。

suppose the mode is 假设模式是

Pet {
  name 
  catagory
  tag  (optional)
}

when I want to create a Pet, I can use post method, the tag can be omitted. 当我要创建宠物时,可以使用post方法,可以省略tag when I want to update a Pet, the problem comes to me. 当我想更新宠物时,问题就来了。 According to the http spec, PUT method will update the entity by replaces the whole resource, which means I need to pass tag parameter. 根据http规范, PUT方法将通过替换整个资源来更新实体,这意味着我需要传递tag参数。 If I didn't pass tag , the default value will be empty, but it will cause the existing tag be override to empty. 如果我没有通过tag ,那么默认值将为空,但是它将导致现有标签被覆盖为空。

For patch method, it will only update partial parameter no matter if it is optional. 对于patch方法,无论是否为可选参数,它只会更新部分参数。 It's clear to understand. 很容易理解。

I don't know if I misunderstand something, currently, in PUT method, I need to figure out what parameter is passed, and then update correspond field. 我不知道我是否误解了,目前,在PUT方法中,我需要弄清楚传递了什么参数,然后更新对应字段。 But this seems the same with PATCH method. 但这与PATCH方法似乎相同。

An important thing to understand is that the HTTP specification describes the semantics (what do the different requests mean), but does not describe the implementation (how do you do it). 需要了解的重要一点是,HTTP规范描述了语义(不同请求的含义),但没有描述实现(如何实现)。 That's deliberate - the specification basically says that your server should pretend to be a key/value store, but it doesn't restrict how you implement that. 这是有意的-规范基本上说您的服务器应该假装为键/值存储,但并不限制您如何实现它。

PUT is roughly analogous to saving a file: "here is an array of bytes, save it using this key". PUT大致类似于保存文件:“这是字节数组,使用此键保存”。 In cases where your storage is a file system, then you just write the array of bytes to disk. 如果您的存储是文件系统,则只需将字节数组写入磁盘。 If your storage is an in memory cache, then you just update your cached copy. 如果您的存储是内存中缓存,则只需更新缓存的副本。

If your storage is some RDBMS database? 如果您的存储是一些RDBMS数据库? Then you have some work to do, identifying which rows in your database need to be changed, and what commands need to be sent to the database to make that happen. 然后,您需要做一些工作,确定需要更改数据库中的哪些行,以及需要向数据库发送哪些命令以实现此目的。

The point is that the client doesn't care -- as a server, you can change your underlying storage from RDBMS to document stores to files systems to whatever, and that's not any of the client's business. 关键是客户端并不在乎-作为服务器,您可以将基础存储从RDBMS更改为文档存储,再将文件系统更改为文件系统,而这与客户端无关。

in PUT method, I need to figure out what parameter is passed, and then update correspond field. 在PUT方法中,我需要找出要传递的参数,然后更新对应的字段。 But this seems the same with PATCH method. 但这与PATCH方法似乎相同。

Yes. 是。 In both cases, you need to figure out how to edit your resource in place. 在这两种情况下,您都需要弄清楚如何在适当位置编辑资源。

PUT may feel a little bit easier, in that it is semantically equivalent to "delete the old version, then create a new version". PUT可能会更容易一点,因为它在语义上等效于“删除旧版本,然后创建新版本”。 You don't have to worry about merging the provided data to the state you already have stored. 您不必担心提供的数据合并到已经存储的状态。

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

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