PUT、POST 和 PATCH 之间有什么区别? [英] What is the difference between PUT, POST and PATCH?

查看:46
本文介绍了PUT、POST 和 PATCH 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP 协议中的 PUT、POST 和 PATCH 方法有什么区别?

What is the difference between PUT, POST and PATCH methods in HTTP protocol?

推荐答案

PUT、POST、GET、DELETE 和 PATCH IN HTTP Verbs 之间的区别:

最常用的 HTTP 动词 POST、GET、PUT、DELETE 类似于数据库中的 CRUD(创建、读取、更新和删除)操作.我们在大写的情况下指定这些 HTTP 动词.所以,下面是它们之间的比较.

The most commonly used HTTP verbs POST, GET, PUT, DELETE are similar to CRUD (Create, Read, Update and Delete) operations in database. We specify these HTTP verbs in the capital case. So, the below is the comparison between them.

  1. 创建 - POST
  2. 读取 - 获取
  3. 更新 - PUT
  4. 删除 - 删除

PATCH: 提交对资源的部分修改.如果您只需要更新资源的一个字段,您可能需要使用 PATCH 方法.

PATCH: Submits a partial modification to a resource. If you only need to update one field for the resource, you may want to use the PATCH method.

注意:
由于 POST、PUT、DELETE 修改了内容,因此使用 Fiddler 对以下 url 进行的测试只是模仿更新.它实际上并没有删除或修改.我们可以通过查看状态码来检查是否有插入、更新、删除.

网址: http://jsonplaceholder.typicode.com/posts/

1) 获取:

GET 是最简单的 HTTP 请求方法类型;浏览器每次单击链接或在地址栏中键入 URL 时使用的那个.它指示服务器将 URL 标识的数据传输到客户端.作为 GET 请求的结果,不应在服务器端修改数据.从这个意义上说,GET 请求是只读的.

GET is the simplest type of HTTP request method; the one that browsers use each time you click a link or type a URL into the address bar. It instructs the server to transmit the data identified by the URL to the client. Data should never be modified on the server side as a result of a GET request. In this sense, a GET request is read-only.

使用 Fiddler 或 PostMan 检查:我们可以使用 Fiddler 来检查响应.打开 Fiddler 并选择 Compose 选项卡.如下所示指定动词和 url,然后单击执行"以检查响应.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

动词: GET

url: http://jsonplaceholder.typicode.com/posts/

响应:您将得到如下响应:

"userId": 1, "id": 1, "title": "sunt aut...", "body": "quia et suscipit..."

"userId": 1, "id": 1, "title": "sunt aut...", "body": "quia et suscipit..."

在happy"(或非错误)路径中,GET 返回 XML 或 JSON 格式的表示以及 HTTP 响应代码 200(OK).在错误情况下,它最常返回 404(未找到)或 400(错误请求).

In the "happy" (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

2) 发布:

POST 动词主要用于创建新资源.特别是,它用于创建从属资源.也就是说,从属于某个其他(例如父)资源.

The POST verb is mostly utilized to create new resources. In particular, it's used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource.

成功创建后,返回 HTTP 状态 201,返回一个 Location 标头,其中包含指向新创建的具有 201 HTTP 状态的资源的链接.

On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status.

使用 Fiddler 或 PostMan 检查:我们可以使用 Fiddler 来检查响应.打开 Fiddler 并选择 Compose 选项卡.如下所示指定动词和 url,然后单击执行"以检查响应.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

动词: POST

url: http://jsonplaceholder.typicode.com/posts/

请求正文:

数据:{标题:'富',身体:'酒吧',用户 ID:1000,编号:1000}

data: { title: 'foo', body: 'bar', userId: 1000, Id : 1000 }

响应:您将收到 201 的响应代码.

Response: You would receive the response code as 201.

如果我们要检查插入的 Id = 1000 的记录,请将动词更改为 Get 并使用相同的 url,然后单击执行.

If we want to check the inserted record with Id = 1000 change the verb to Get and use the same url and click Execute.

前面说过,上面的url只允许读取(GET),我们不能真正读取更新的数据.

As said earlier, the above url only allows reads (GET), we cannot read the updated data in real.

3) 放置:

PUT 最常用于更新功能,PUT 到已知资源 URI,请求正文包含原始资源的最新更新表示.

PUT is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.

使用 Fiddler 或 PostMan 检查:我们可以使用 Fiddler 来检查响应.打开 Fiddler 并选择 Compose 选项卡.如下所示指定动词和 url,然后单击执行"以检查响应.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

动词: PUT

网址: http://jsonplaceholder.typicode.com/posts/1

请求正文:

数据:{标题:'富',身体:'酒吧',用户 ID:1,编号:1}

data: { title: 'foo', body: 'bar', userId: 1, Id : 1 }

响应:成功更新后,它会从 PUT 返回 200(如果未返回正文中的任何内容,则返回 204).

Response: On successful update it returns 200 (or 204 if not returning any content in the body) from a PUT.

4) 删除:

DELETE 很容易理解.它用于删除由 URI 标识的资源.

DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.

成功删除后,返回 HTTP 状态 200 (OK) 以及响应正文,可能是已删除项目的表示(通常需要太多带宽),或包装的响应(请参阅下面的返回值).要么返回 HTTP 状态 204(NO CONTENT),没有响应正文.换句话说,没有正文的 204 状态,或 JSEND 样式的响应和 HTTP 状态 200 是推荐的响应.

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

使用 Fiddler 或 PostMan 检查:我们可以使用 Fiddler 来检查响应.打开 Fiddler 并选择 Compose 选项卡.如下所示指定动词和 url,然后单击执行"以检查响应.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

动词:删除

网址: http://jsonplaceholder.typicode.com/posts/1

响应:成功删除后,它会返回 HTTP 状态 200(正常)以及响应正文.

Response: On successful deletion it returns HTTP status 200 (OK) along with a response body.

PUT 和 PATCH 之间的示例

PUT

如果我必须更改我的名字,则发送 PUT 请求以进行更新:

If I had to change my firstname then send PUT request for Update:

{ "first": "Nazmul", "last": "hasan" }所以,这里为了更新名字我们需要再次发送数据的所有参数.

{ "first": "Nazmul", "last": "hasan" } So, here in order to update the first name we need to send all the parameters of the data again.

补丁:

补丁请求表示我们只会发送我们需要修改的数据,而不修改或影响数据的其他部分.例如:如果我们只需要更新名字,我们只传递名字.

Patch request says that we would only send the data that we need to modify without modifying or effecting other parts of the data. Ex: if we need to update only the first name, we pass only the first name.

请参考以下链接了解更多信息:

Please refer the below links for more information:

https://jsonplaceholder.typicode.com/

https://github.com/typicode/jsonplaceholder#how-to

PATCH 和 PUT 的主要区别是什么请求?

http://www.restapitutorial.com/lessons/httpmethods.html

这篇关于PUT、POST 和 PATCH 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆