简体   繁体   English

在 rest API 中使用 POST 方法而不是 GET

[英]Using POST method instead of GET in a rest API

是否存在我们使用 POST 而不是 GET 来实现 get 操作功能的特定场景?

GET is supposed to get :) and POST is used to mainly add something new or sometimes often used for updates as well (although PUT is recommended in such scenarios). GET应该得到 :) 而POST主要用于添加新内容或有时也经常用于更新(尽管在这种情况下建议使用PUT )。 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.没有我们使用POST而不是GET特定场景,如果我们需要这样做,那意味着我们可能做错了,尽管没有什么可以阻止您这样做,但这是糟糕的设计,您应该退后一步并计划您的API 仔细。

There are 2 important cases for a POST ie 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. POST有两种重要的情况,即 POST 比GET更安全,并且POST可以发送大量数据,但即使如此,我也不建议为什么要使用POST来模拟GET行为。

Lets understand usage of get and post :让我们了解 get 和 post 的用法:

What is GET Method?什么是GET方法?

It appends form-data to the URL in name/ value pairs.它将表单数据以名称/值对的形式附加到 URL。 The length of the URL is limited by 2048 characters. URL 的长度限制为 2048 个字符。 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.它不能用于发送二进制数据,如图像或 Word 文档。 It also provides $_GET associative array to access all the sent information using the GET method.它还提供了 $_GET 关联数组以使用 GET 方法访问所有发送的信息。

What is POST Method?什么是POST方法?

It appends form-data to the body of the HTTP request in such a way that data is not shown in the URL.它将表单数据附加到 HTTP 请求的正文中,这样数据就不会显示在 URL 中。 This method does not have any restrictions on data size to be sent.此方法对要发送的数据大小没有任何限制。 Submissions by form with POST cannot be bookmarked.不能为通过 POST 表单提交的内容添加书签。 This method can be used to send ASCII as well as binary data like image and word documents.此方法可用于发送 ASCII 以及二进制数据,如图像和 Word 文档。 Data sent by the POST method goes through HTTP header so security depends on the HTTP protocol. POST 方法发送的数据通过 HTTP 标头,因此安全性取决于 HTTP 协议。 You have to know that your information is secure by using secure HTTP.您必须通过使用安全 HTTP 知道您的信息是安全的。 This method is a little safer than GET because the parameters are not stored in browser history or in web server logs.这种方法比 GET 安全一些,因为参数不会存储在浏览器历史记录或 Web 服务器日志中。 It also provides $_POST associative array to access all the sent information using the POST method.它还提供了 $_POST 关联数组以使用 POST 方法访问所有发送的信息。

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

So both the methods have their specific usage.所以这两种方法都有其特定的用法。

POST method is used to send data to a server to create or update a resource. POST 方法用于将数据发送到服务器以创建或更新资源。

GET method is used to request data from a specified resource. GET 方法用于从指定资源请求数据。

If you want to fetch some data you can use the GET method.如果你想获取一些数据,你可以使用 GET 方法。 But if you want to update an existing resource or create any new resource you should use POST.但是,如果您想更新现有资源或创建任何新资源,您应该使用 POST。 GET will not help you to create/update resources. GET 不会帮助您创建/更新资源。 So exposing the api should be specific to your needs.因此,公开 api 应该特定于您的需求。

UPDATE更新

So your main question is in what scenario we can use POST to implement the functionality of GET.所以你的主要问题是在什么场景下我们可以使用 POST 来实现 GET 的功能。

To answer that, as you understand what GET and POST does, so with GET request you will only fetch the resource.要回答这个问题,正如您了解 GET 和 POST 的作用一样,因此对于 GET 请求,您将只获取资源。 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.但是通过 POST 请求,您正在创建或更新资源,并且还可以在相同的请求响应场景中发送包含表单数据的响应正文。 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.因此,假设您正在创建一个新资源和您想要查看的相同资源,而不是先进行 POST 调用然后再次进行 GET 调用以获取相同的资源,这将花费额外的开销。 You can skip the GET call and see your desired response from the POST response itself.您可以跳过 GET 调用并从 POST 响应本身查看所需的响应。 This is the scenario you can use POST instead of making an extra GET call.在这种情况下,您可以使用 POST 而不是进行额外的 GET 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM