rest - 使用 POST 而不是 PUT REST

标签 rest

POST:- 用于创建和更新资源
PUT:-用于更新现有资源

我可以使用 POST 代替 PUT 方法吗?如果我使用 POST 方法而不是 PUT 方法,会有什么缺点?

如果 POST 可以完成 PUT 方法的工作,为什么还需要 PUT 方法?

最佳答案

Can I use POST instead of PUT method?

是的,可以。例如,HTML 表单对所有写入使用 POST。

If POST can do work of PUT method, why PUT method is required?

以前不是这样的。在 HTTP/1.0 ,指定的方法是 HEAD、GET 和 POST。 PUT 降级为 Appendix D: Additional Features .

If I use POST method over PUT method, what will be disadvantages?

PUT 为 idempotent 。 POST 不是。

A request method is considered "idempotent" if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request.....

the idempotent property only applies to what has been requested by the user; a server is free to log each request separately, retain a revision control history, or implement other non-idempotent side effects for each idempotent request.

Idempotent methods are distinguished because the request can be repeated automatically if a communication failure occurs before the client is able to read the server's response. For example, if a client sends a PUT request and the underlying connection is closed before any response is received, then the client can establish a new connection and retry the idempotent request. It knows that repeating the request will have the same intended effect, even if the original request succeeded, though the response might differ.

这意味着对于 PUT,客户端可以使用至少一次传递请求;通过不可靠的网络重复发送相同的 PUT 消息,直到收到响应。

(此保证由服务器提供,通过给定资源接受 PUT 消息这一事实来传达。它不是免费的,您需要确保服务器正确处理消息。)

请注意,不仅客户端知道此保证,所有可以看到请求消息的中间组件(代理)也都知道 - 代理不需要返回浏览器来询问用户是否可以安全地重试该消息 - PUT 方法表示服务器正在提供安全保证。

关于rest - 使用 POST 而不是 PUT REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258725/

相关文章:

java - 为什么在启动 Spring Boot 应用程序时会出现 Gson 构建器错误?

php - Rest API 404 页面未找到

wcf - 保护 REST API/Web 服务的最佳实践

rest - $skiptoken 不会使用 Sharepoint API 跳过并返回下一组记录

java - Spring Rest 客户端希望看到错误消息而不是异常

rest - SharePoint REST 请求选择所有列并展开查找列

rest - 在没有用户委托(delegate)的情况下如何使用OAuth? -- 微软图形API

scala - scala 中的 RESTful http DELETE 方法(玩 2.0)

javascript - JS+REST+NodeJS : How to parse a JSON list from a JSON object

rest - CouchDB - CouchApp 的任何替代品?