POST 和 PUT HTTP REQUEST 之间有什么区别? [英] What's the difference between a POST and a PUT HTTP REQUEST?

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

问题描述

它们似乎都在向体内的服务器发送数据,那么它们的不同之处是什么?

They both seem to be sending data to the server inside the body, so what makes them different?

推荐答案

HTTP PUT:

PUT 将文件或资源放置在特定的 URI 中,并且正好位于该 URI 处.如果该 URI 上已有文件或资源,PUT 将替换该文件或资源.如果那里没有文件或资源,PUT 会创建一个.PUT 是幂等,但矛盾的是,PUT 响应不是可缓存.

PUT puts a file or resource at a specific URI, and exactly at that URI. If there's already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one. PUT is idempotent, but paradoxically PUT responses are not cacheable.

PUT 的 HTTP 1.1 RFC 位置

HTTP POST:

POST 将数据发送到特定 URI 并期望该 URI 处的资源来处理请求.此时 Web 服务器可以确定如何处理指定资源上下文中的数据.POST 方法不是 幂等,但是 POST 响应 可以缓存,只要服务器设置了适当的 Cache-Control 和 Expires 标头.

POST sends data to a specific URI and expects the resource at that URI to handle the request. The web server at this point can determine what to do with the data in the context of the specified resource. The POST method is not idempotent, however POST responses are cacheable so long as the server sets the appropriate Cache-Control and Expires headers.

官方 HTTP RFC 指定 POST 为:

The official HTTP RFC specifies POST to be:

  • 现有资源的注释;
  • 向公告板、新闻组、邮件列表发布消息,或类似的文章组;
  • 提供一个数据块,比如提交一个数据的结果形成数据处理过程;
  • 通过追加操作扩展数据库.

用于 POST 的 HTTP 1.1 RFC 位置

POST 和 PUT 的区别:

RFC 本身解释了核心区别:

The RFC itself explains the core difference:

两者的根本区别POST 和 PUT 请求体现在的不同含义请求-URI.POST 请求中的 URI标识将要使用的资源处理封闭的实体.那资源可能是一个数据接受进程,通往其他一些协议,或一个单独的实体接受注释.相比之下,PUT 请求中的 URI 标识包含在请求中的实体——用户代理知道 URI 是什么预期和服务器不得尝试将请求应用于某些其他资源.如果服务器需要将该请求应用于不同的 URI,它必须发送 301(永久移动)响应;然后用户代理可以它自己决定是否重定向请求.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

另外,更简洁一点,RFC 7231 第 4.3 节.4 PUT 状态(强调),

Additionally, and a bit more concisely, RFC 7231 Section 4.3.4 PUT states (emphasis added),

4.3.4.放

PUT 方法请求目标资源的状态为createdreplaced 用表示定义的状态包含在请求消息负载中.

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.

使用正确的方法,无关紧要:

REST ROA 与 SOAP 相比的一个好处是,当使用 HTTP REST ROA 时,它鼓励HTTP 动词/方法的正确用法.因此,例如,当您想在该确切位置创建资源时,您只会使用 PUT.而且您永远不会使用 GET 来创建或修改资源.

One benefit of REST ROA vs SOAP is that when using HTTP REST ROA, it encourages the proper usage of the HTTP verbs/methods. So for example you would only use PUT when you want to create a resource at that exact location. And you would never use GET to create or modify a resource.

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

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