HTTP协议中的PUT、POST和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] |
其他回答
这样想吧……
POST - create
PUT -替换
补丁-更新
GET - read
DELETE -删除
下面的定义来自真实世界的例子。
示例概述 对于每个客户端数据,我们存储一个标识符来查找客户端数据,并将该标识符发送回客户端以供参考。
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方法中,如果没有找到标识符,则抛出异常。
如果您对上述内容有任何疑问,请告诉我。
请求类型
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
资源: 幂等性——什么是幂等性?
这里是一个简单的描述:
POST总是用于创建资源(是否复制并不重要) PUT用于检查资源是否存在,然后更新,否则创建新的资源 PATCH总是用于更新资源
PUT =用所提供的新表示替换整个RESOURCE
PATCH =用所提供的值替换源资源的部分和|或资源的其他部分被更新,你没有提供(时间戳)和|或更新资源影响其他资源(关系)
https://laracasts.com/discuss/channels/general-discussion/whats-the-differences-between-put-and-patch?page=1
推荐文章
- 什么是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