为什么我应该 POST 数据而不是 GET? [英] Why should I POST data rather than GET?

查看:18
本文介绍了为什么我应该 POST 数据而不是 GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您不想发布带有用户名和密码的表单,以便任何人都可以使用历史记录查看或可能不需要重复操作的情况(刷新页面 = 将项目添加到购物车可能不是需要).所以我知道什么时候我可能想使用一个而不是另一个.但是我总是可以让服务器在 GET 之后重定向 URL 以解决购物车问题,也许我的大多数表单都可以与 GET 完美配合.

为什么我应该使用 POST 而不是 GET?我不明白一个比另一个的好处.我确实注意到 POST 不会向历史记录/URL 添加数据,并且会警告您刷新页面,但这是我所知道的仅有的两个不同之处.为什么作为开发人员,我可能想要使用其中一种?

解决方案

每个 HTTP 方法:POST、GET、PUT、DELETE 等都有自己的语义.在选择正确的方法时,重要的是要了解 HTTP 和 REST,因为它是 HTTP 的方式是为了工作以及底层网络基础设施的运行方式.

此处提供了关于 REST 的优秀教程.以下是关于 POST 和 GET 方法的说明:

  • 因为每个资源都使用相同的接口,所以您可以依赖于能够使用 GET 检索表示——即它的一些呈现.因为 GET 的语义是在规范中定义的,所以你可以确定你在调用它时没有义务——这就是该方法被称为安全"的原因.GET 支持非常高效和复杂的缓存,因此在许多情况下,您甚至不必向服务器发送请求.您还可以确定 GET 是幂等的——如果您发出 GET 请求但没有得到结果,您可能不知道您的请求是否从未到达目的地,或者响应在返回给您的途中丢失了.

    幂等性保证意味着您可以简单地再次发出请求.PUT(这基本上意味着用这个数据更新这个资源,或者如果它已经不存在就在这个 URI 上创建它")和 DELETE(你可以简单地一次又一次地尝试直到你得到结果——删除)也保证幂等性不存在的东西没有问题).POST,通常表示创建新资源",也可用于调用任意处理,因此既不安全也不幂等.

I know you don't want to POST a form with a username and password where anyone could use the history to see or situations where repeat actions may not be desired (refreshing a page = adding an item to a cart may not be desired). So I have an understanding when I may want to use one over the other. But I could always have the server redirect the URL after a GET to get around the cart problem and maybe most of my forms will work perfectly fine with GET.

Why should I use POST over GET? I don't understand the benefits of one over the other. I do notice POST doesn't add data to the history/URL and will warn you about refreshing the page, but those are the only two differences I know of. Why as a developer might I want to use one over the other?

解决方案

Every HTTP method: POST, GET, PUT, DELETE, etc. caries its own semantics. When choosing the right method it is important to understand the HTTP and REST, since it is the way HTTP was meant to work and the way underlying network infrastructure operates.

A good tutorial on REST is available here. Here is what is says about POST and GET methods:

  • Because the same interface is used for every resource, you can rely on being able to retrieve a representation — i.e., some rendering of it — using GET. Because GET’s semantics are defined in the specification, you can be sure that you have no obligations when you call it — this is why the method is called "safe". GET supports very efficient and sophisticated caching, so in many cases, you don’t even have to send a request to the server. You can also be sure that a GET is idempotent — if you issue a GET request and don’t get a result, you might not know whether your request never reached its destination or the response got lost on its way back to you.

    The idempotence guarantee means you can simply issue the request again. Idempotence is also guaranteed for PUT (which basically means "update this resource with this data, or create it at this URI if it’s not there already") and for DELETE (which you can simply try again and again until you get a result — deleting something that’s not there is not a problem). POST, which usually means "create a new resource", can also be used to invoke arbitrary processing and thus is neither safe nor idempotent.

这篇关于为什么我应该 POST 数据而不是 GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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