简体   繁体   English

我应该为REST服务使用哪种HTTP方法?

[英]Which HTTP method should I use for my REST service?

You might have seen many post related to this title. 您可能已经看过很多与此标题相关的帖子。 But, I think, I have a different case where I need help. 但是,我认为,在其他情况下,我需要帮助。 I tried to go thru several threads for my question but could not find any specific answer that I can go with confidence. 我试图通过几个线程来回答我的问题,但是找不到我可以放心的任何具体答案。

To explain, I want to publish a RESTful API that performs typical GET operation based on provided input details. 为了说明,我想发布一个RESTful API,该API根据提供的输入详细信息执行典型的GET操作。 This API is idempotent in nature and does not create, update or delete any resource. 该API本质上是幂等的,并且不会创建,更新或删除任何资源。 So, my ideal choice should be to use HTTP GET method in this case. 因此,在这种情况下,我的理想选择是使用HTTP GET方法。

However, making things complex, my API will need a big JSON object with details as a part of the request to derive required information for the response. 但是,使事情变得复杂的是,我的API将需要一个带有详细信息的大JSON对象作为请求的一部分,以获取响应所需的信息。 It is not advisable to put that JSON string in the request URI. 不建议将该JSON字符串放入请求URI中。 Hence, I have to put that in request body itself. 因此,我必须将其放入请求正文中。 But, HTTP GET does not consider request body content. 但是,HTTP GET不考虑请求正文内容。 And, I don't want to (or don't know if I should) use HTTP POST or PUT for this API as the API is idempotent and does not create/modify any resource. 而且,我不想(或不知道是否应该)为此API使用HTTP POST或PUT,因为该API是幂等的,并且不会创建/修改任何资源。

For now, I am more inclined towards using HTTP OPTIONS for this purpose, but I am not sure if this is something I should do considering RESTful standardization. 目前,我更倾向于为此目的使用HTTP OPTIONS,但是我不确定是否应该考虑使用RESTful标准化。

So, what do you suggest? 所以你有什么建议? Is it an acceptable usecase to use POST or OPTIONS in this case? 在这种情况下使用POST或OPTIONS是否可以接受? Or, I should do something else? 还是我应该做些其他的事情?

For reference, below is the dummy sample of the request data object. 作为参考,以下是请求数据对象的虚拟样本。

{
"parametersList": {
    "itemRange": {
        "code": "11",
        "start": 100683,
        "end": 168003
    },
    "dateRange": {
        "startDate": "2017-10-01",
        "endDate": "2017-10-31"
    },
    "market": {....},
    "startTimeRange": {...},
    "endTimeRange": {....},
    "serviceType": {....},
    "segmentType": {....},
    "daysOfWeek": {
        "days": ["MON", "WED", "THU", "FRI", "SAT", "SUN"]
    },
    "itemNumber": 0
}
}

The {...} in above example represents internal object structure that may vary based on the data. 上面示例中的{...}表示内部对象结构,该结构可能会根据数据而有所不同。

In such cases you should use POST. 在这种情况下,您应该使用POST。 In general POST is used if no other method is applicable. 通常,如果没有其他方法适用,则使用POST。 OPTIONS has another purpose and cannot be used here. 选项还有另一个目的,不能在这里使用。

POST is for example also used for large search queries where you specify search terms in the body 例如,POST也用于大型搜索查询,您可以在其中指定搜索词

Another good example for using POST instead of GET is when you are sending sensitive data in the request. 使用POST而不是GET的另一个很好的例子是在请求中发送敏感数据时。

also have a look at this article 也看看这篇文章

and this question 这个问题

Go for POST, even if you manage to get it done with GET(creating a URI) its not the optimal solution as there might be cases when your GET request URL can become too long. 即使您设法通过GET(创建URI)完成它,它也不是最佳解决方案,因为在某些情况下,您的GET请求URL可能会变得太长。

Moreover I didn't find any viable source discouraging the use of POST for querying with a valid reason. 此外,我没有找到任何可行的来源来阻止使用POST进行正当查询。

POST(with some other security measures) is used for authentication as well, which does not create/modify any resource. POST(以及其他一些安全措施)也用于身份验证,它不会创建/修改任何资源。 So using it for your problem shall not be a bad practice. 因此,将其用于您的问题将不是一个坏习惯。

This is an interesting one because of your requirement to send a big JSON object. 这很有趣,因为您需要发送一个大的JSON对象。

A REST API endpoint that gets a resource should always use a GET endpoint with GET parameters. 获取资源的REST API端点应始终使用带有GET参数的GET端点。 Is there a specific reason why you are trying to send a JSON object rather than sending parameters? 为什么要尝试发送JSON对象而不是发送参数有特定原因吗?

In the case that you insist on sending a JSON object, I have found examples of existing code that base64 encode the JSON object and then add it to a GET parameter. 在您坚持要发送JSON对象的情况下,我发现了现有代码的示例,这些代码对base64 JSON对象进行编码,然后将其添加到GET参数中。 However please note that if you try to send a massive JSON object you could have issues with URL length. 但是请注意,如果您尝试发送大量的JSON对象,则URL长度可能会出现问题。 But it should never come to that. 但这绝不应该如此。 GET parameters on a restful API should always be simple and short. 宁静的API上的GET参数应始终简短。 If you are sending too much data then there's something wrong with the design of the REST API and it needs to be thought through more carefully. 如果发送的数据过多,则REST API的设计存在问题,需要仔细考虑。

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

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