HTTP协议中的PUT、POST和PATCH方法有什么区别?
当前回答
PUT和PATCH请求的主要区别:
假设我们有一个资源,其中包含一个人的姓和名。
如果我们想要改变名字,那么我们发送一个put请求Update
{"第一个":"迈克尔","最后一个":"安吉洛"}
在这里,虽然我们只改变了名字,但是对于PUT请求,我们必须在第一个和最后一个发送参数。 换句话说,必须再次发送所有值,即完整的有效负载。
然而,当我们发送PATCH请求时,我们只发送我们想要更新的数据。换句话说,我们只发送名字来更新,不需要发送姓氏。
其他回答
PUT和PATCH请求的主要区别:
假设我们有一个资源,其中包含一个人的姓和名。
如果我们想要改变名字,那么我们发送一个put请求Update
{"第一个":"迈克尔","最后一个":"安吉洛"}
在这里,虽然我们只改变了名字,但是对于PUT请求,我们必须在第一个和最后一个发送参数。 换句话说,必须再次发送所有值,即完整的有效负载。
然而,当我们发送PATCH请求时,我们只发送我们想要更新的数据。换句话说,我们只发送名字来更新,不需要发送姓氏。
下面的定义来自真实世界的例子。
示例概述 对于每个客户端数据,我们存储一个标识符来查找客户端数据,并将该标识符发送回客户端以供参考。
POST If the client sends data without any identifier, then we will store the data and assign/generate a new identifier. If the client again sends the same data without any identifier, then we will store the data and assign/generate a new identifier. Note: Duplication is allowed here. PUT If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will create a resource with the data and assign/generate a new identifier. PATCH If the client sends data with an identifier, then we will check whether that identifier exists. If the identifier exists, we will update the resource with the data, else we will throw an exception.
注意:在PUT方法中,如果没有找到标识符,我们不会抛出异常。但是在PATCH方法中,如果没有找到标识符,则抛出异常。
如果您对上述内容有任何疑问,请告诉我。
你可能会把restful HTTP方法理解为javascript中对数组的相应操作(索引偏移量为1)。
请看下面的例子:
Method | Url | Meaning |
---|---|---|
GET | /users | return users array |
GET | /users/1 | return users[1] object |
POST | /users | users.push(body) ; return last id or index |
PUT | /users | replace users array |
PUT | /users/1 | users[1] = body |
PATCH | /users/1 | users[1] = {...users[1], ...body } |
DELETE | /users/1 | delete users[1] |
参考RFC: https://www.rfc-editor.org/rfc/rfc9110.html#name-method-definitions
POST -创建新对象 PUT -更新旧对象或创建新对象(如果不存在) 更新/修改旧对象。主要用于修改。
之前提到的RFC的解释很少,但如果你仔细阅读,你会注意到PUT和PATCH方法是在POST之后出现的,这是一种常见的创建原生HTML表单的老式方法。
因此,如果你试图支持所有的方法(如PATCH或DELETE),可以建议使用所有方法的最合适的方法是坚持CRUD模型:
Create - PUT 阅读-获取 更新-补丁 删除-删除
旧的HTML原生方式: 阅读-获取 创建/更新/删除- POST
程序员们好运!: -)
请求类型
create - POST read - GET create或update - PUT delete -删除 update - PATCH
GET/PUT是等幂的 PATCH有时是等幂的
什么是幂等的 这意味着如果我们多次触发查询,它不应该影响查询的结果。(相同的输出。假设一头牛怀孕了,如果我们再次繁殖它,那么它就不能怀孕多次)
得到:
简单的会。从服务器获取数据并显示给用户
{
id:1
name:parth
email:x@x.com
}
职位:
在数据库中创建新资源。这意味着它会添加新的数据。它不是等幂的。
将:-
创建新资源,否则添加到现有资源。 幂等的,因为它每次都会更新相同的资源,输出也会相同。 前女友。 -初始数据
{
id:1
name:parth
email:x@x.com
}
执行put-localhost / 1 把电子邮件:ppp@ppp.com
{
id:1
email:ppp@ppp.com
}
补丁
现在是补丁请求 PATCH有时是等幂的
id:1
name:parth
email:x@x.com
}
块名称:w
{
id:1
name:w
email:x@x.com
}
HTTP Method GET yes POST no PUT yes PATCH no* OPTIONS yes HEAD yes DELETE yes
资源: 幂等性——什么是幂等性?
推荐文章
- 什么是HTTP“主机”报头?
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- 如何阻止恶意代码欺骗“Origin”报头来利用CORS?
- 为什么说“HTTP是无状态协议”?
- 我需要HTTP GET请求的内容类型报头吗?
- 如何让Chrome允许混合内容?
- 正确的方式删除cookies服务器端
- REST DELETE真的是幂等的吗?
- 了解Chrome网络日志“停滞”状态
- 用户代理字符串可以有多大?
- 什么是接受* HTTP报头q=0.5 ?
- HTTP状态码200(缓存)和状态码304之间有什么区别?
- HTTP POST返回错误:417“期望失败。”
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows