Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

http - POST和PUT HTTP REQUEST有什么区别?(What's the difference between a POST and a PUT HTTP REQUEST?)

它们似乎都在将数据发送到体内的服务器,那么什么使它们与众不同?

  ask by fuentesjr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

HTTP PUT:

(HTTP PUT:)

PUT puts a file or resource at a specific URI, and exactly at that URI.

(PUT将文件或资源放在特定的URI上,并确切地放在该URI上。)

If there's already a file or resource at that URI, PUT replaces that file or resource.

(如果该URI上已经有文件或资源,则PUT会替换该文件或资源。)

If there is no file or resource there, PUT creates one.

(如果那里没有文件或资源,PUT将创建一个。)

PUT is idempotent , but paradoxically PUT responses are not cacheable.

(PUT是幂等的 ,但矛盾的是PUT响应不可缓存。)

HTTP 1.1 RFC location for PUT

(PUT的HTTP 1.1 RFC位置)

HTTP POST:

(HTTP POST:)

POST sends data to a specific URI and expects the resource at that URI to handle the request.

(POST将数据发送到特定的URI,并期望该URI上的资源可以处理请求。)

The web server at this point can determine what to do with the data in the context of the specified resource.

(此时,Web服务器可以确定在指定资源的上下文中如何处理数据。)

The POST method is not idempotent , however POST responses are cacheable so long as the server sets the appropriate Cache-Control and Expires headers.

(POST方法不是幂等的 ,但是只要服务器设置适当的Cache-Control和Expires标头,POST响应就可以缓存。)

The official HTTP RFC specifies POST to be:

(官方HTTP RFC指定POST为:)

  • Annotation of existing resources;

    (注释现有资源;)

  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles;

    (将消息发布到公告板,新闻组,邮件列表或类似的文章组;)

  • Providing a block of data, such as the result of submitting a form, to a data-handling process;

    (向数据处理过程提供数据块,例如提交表单的结果;)

  • Extending a database through an append operation.

    (通过附加操作扩展数据库。)

HTTP 1.1 RFC location for POST

(POST的HTTP 1.1 RFC位置)

Difference between POST and PUT:

(POST和PUT之间的区别:)

The RFC itself explains the core difference:

(RFC本身解释了核心差异:)

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI.

(POST和PUT请求之间的根本区别体现在Request-URI的不同含义上。)

The URI in a POST request identifies the resource that will handle the enclosed entity.

(POST请求中的URI标识将处理封闭实体的资源。)

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.

(相比之下,PUT请求中的URI标识请求中包含的实体-用户代理知道要使用的URI,并且服务器绝不能尝试将请求应用于其他资源。)

If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response;

(如果服务器希望将该请求应用于其他URI,则它必须发送301(永久移动)响应;)

the user agent MAY then make its own decision regarding whether or not to redirect the request.

(然后,用户代理可以自行决定是否重定向请求。)

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

(此外,更简洁一点的是RFC 7231第4.3.4节PUT状态(添加了强调),)

4.3.4.

(4.3.4。)

PUT

()

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.

(PUT方法请求created目标资源的状态或replaced为请求消息有效负载中包含的表示形式所定义的状态。)

Using the right method, unrelated aside:

(使用正确的方法,除了无关的:)

One benefit of REST ROA vs SOAP is that when using HTTP REST ROA, it encourages the proper usage of the HTTP verbs/methods.

(REST ROA vs SOAP的一个好处是,当使用HTTP REST ROA时,它鼓励正确使用HTTP动词/方法。)

So for example you would only use PUT when you want to create a resource at that exact location.

(因此,例如,仅当您想在该确切位置创建资源时才使用PUT。)

And you would never use GET to create or modify a resource.

(而且,您永远不会使用GET创建或修改资源。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...