使用POST方法替代GET方法在一个REST API中。

huangapple go评论69阅读模式
英文:

Using POST method instead of GET in a rest API

问题

在实现获取操作的功能时,是否有特定情况需要使用POST而不是GET?

英文:

Is there a specific scenario where we use a POST instead of GET, to implement the functionality of get operation ?

答案1

得分: 4

GET 被用来获取 :),而 POST 则主要用于添加新内容,有时也经常用于更新(尽管在这种情况下推荐使用 PUT)。没有特定的情况可以替代使用 POST 而不是 GET,如果我们需要这样做,那可能意味着我们的做法有问题。尽管没有什么阻止你这样做,但这是不良设计,你应该退一步,仔细规划你的 API。

有两个重要情况适合使用 POST,即 POSTGET 更安全,而且 POST 可以发送大量数据,但即使如此,我也不建议使用 POST 来模拟 GET 的行为。

英文:

GET is supposed to get 使用POST方法替代GET方法在一个REST API中。 and POST is used to mainly add something new or sometimes often used for updates as well (although PUT is recommended in such scenarios). There is no specific scenario where we use a POST instead of a GET, if we require this, that means we are probably doing it wrong, although nothing stops you doing this but this is bad design and you should take a step back and plan your API carefully.

There are 2 important cases for a POST i.e. POST is more secure than a GET and POST can send large amount of data but even with this I won't recommend why one will use POST to simulate a GET behaviour.

答案2

得分: 1

理解使用 GET 和 POST 方法:

什么是 GET 方法?

它将表单数据以名称/值对的形式附加到 URL。URL 的长度限制为 2048 个字符。如果您要发送密码或某些敏感信息到服务器,不应使用此方法。它用于提交表单,用户可以将结果添加到书签中。适用于不太安全的数据。它不能用于发送像图片或 Word 文档之类的二进制数据。它还提供 $_GET 关联数组,以使用 GET 方法访问所有发送的信息。

什么是 POST 方法?

它以一种不在 URL 中显示数据的方式,将表单数据附加到 HTTP 请求的正文中。此方法对要发送的数据大小没有任何限制。通过 POST 提交的表单无法添加到书签中。此方法可用于发送 ASCII 和二进制数据,如图像和 Word 文档。通过 POST 方法发送的数据经过 HTTP 标头,因此安全性取决于 HTTP 协议。您必须知道通过使用安全的 HTTP 协议来确保信息安全。此方法比 GET 稍安全,因为参数不会存储在浏览器历史记录或 Web 服务器日志中。它还提供 $_POST 关联数组,以使用 POST 方法访问所有发送的信息。

来源:https://www.edureka.co/blog/get-and-post-method/

因此,这两种方法都有各自特定的用途。

英文:

Lets understand usage of get and post :

What is GET Method?

It appends form-data to the URL in name/ value pairs. The length of the URL is limited by 2048 characters. This method must not be used if you have a password or some sensitive information to be sent to the server. It is used for submitting the form where the user can bookmark the result. It is better for data that is not secure. It cannot be used for sending binary data like images or word documents. It also provides $_GET associative array to access all the sent information using the GET method.

What is POST Method?

It appends form-data to the body of the HTTP request in such a way that data is not shown in the URL. This method does not have any restrictions on data size to be sent. Submissions by form with POST cannot be bookmarked. This method can be used to send ASCII as well as binary data like image and word documents. Data sent by the POST method goes through HTTP header so security depends on the HTTP protocol. You have to know that your information is secure by using secure HTTP. This method is a little safer than GET because the parameters are not stored in browser history or in web server logs. It also provides $_POST associative array to access all the sent information using the POST method.

Source: https://www.edureka.co/blog/get-and-post-method/

So both the methods have their specific usage.

答案3

得分: 0

POST方法用于向服务器发送数据以创建或更新资源。

GET方法用于从指定资源请求数据。

如果您想获取一些数据,可以使用GET方法。但是,如果您想更新现有资源或创建任何新资源,应该使用POST。GET不会帮助您创建/更新资源。因此,暴露API应根据您的需求进行具体设置。

更新

因此,您的主要问题是在什么情况下我们可以使用POST来实现GET的功能。

为了回答这个问题,由于您已经理解了GET和POST的作用,所以通过GET请求,您只会获取资源。但是通过POST请求,您正在创建或更新资源,并且还可以在同一请求响应场景中发送包含表单数据的响应正文。因此,假设您要创建一个新资源,并且您想要查看相同的资源,您可以跳过首先进行POST调用,然后再进行GET调用以获取相同资源,以节省额外的开销。您可以跳过GET调用,直接从POST响应本身中查看所需的响应。这就是您可以在不需要额外进行GET调用的情况下使用POST的情景。

英文:

POST method is used to send data to a server to create or update a resource.

GET method is used to request data from a specified resource.

If you want to fetch some data you can use the GET method. But if you want to update an existing resource or create any new resource you should use POST. GET will not help you to create/update resources. So exposing the api should be specific to your needs.

UPDATE

So your main question is in what scenario we can use POST to implement the functionality of GET.

To answer that, as you understand what GET and POST does, so with GET request you will only fetch the resource. But with POST request you are creating or updating the resource and also can send the response body containing the form data in the same request response scenario. So suppose you are creating a new resource and the same resource you want to see, instead of making a POST call first and making a GET call again to fetch the same resource will cost extra overhead. You can skip the GET call and see your desired response from the POST response itself. This is the scenario you can use POST instead of making an extra GET call.

huangapple
  • 本文由 发表于 2020年9月30日 10:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64130190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定