在 GET 期间是否有规范/RESTful 方式将查询详细信息发送到服务器? [英] Is there a canonical/RESTful way to send query details to a server during a GET?

查看:16
本文介绍了在 GET 期间是否有规范/RESTful 方式将查询详细信息发送到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个(或多或少)在 ASP.NET 和 IIS 上运行的 RESTful 内部 Web 服务.我希望客户端能够在访问大量条目时将查询详细信息传递给服务器,使用 JSON 以已知方式描述查询.问题是发送到服务器的查询会很复杂;它们可能包括聚合、过滤、映射——基本上是 支持的任何内容LINQ 查询运算符.这将导致表示查询的 JSON 对象相对较大.

I'm designing a (more or less) RESTful internal web service running on ASP.NET and IIS. I want clients to be able to pass query details to the server when accessing large collections of entries, using JSON to describe the query in a known manner. The issue is that the queries sent to the server will be complex; they may include aggregation, filtering, mapping—essentially anything that is supported by the LINQ query operators. This will result in relatively large JSON objects representing the queries.

我面临的冲突是,虽然在 REST 世界中查询在语义上是 GET,但没有标准化的方法在 GET代码>获取.我想出了几个办法来解决这个问题.

The conflict I'm facing is that, while a query is semantically a GET in the world of REST, there's no standardized way to pass a large block of data to a web server during a GET. I've come up with a few options to get around this issue.

选项 1:GET 请求的正文中发送查询对象.

Option 1: Send the query object in the body of the GET request.

GET /namespace/collection/ HTTP/1.1
Content-Length: 22

{ /* query object */ }

显然,这是非标准的,某些软件可能会因具有正文的 GET 请求而窒息.(或者更糟的是,简单地剥离主体并在没有它的情况下处理请求,这会导致服务器返回错误的结果集.)

Obviously, this is non-standard, and some software may choke on a GET request that has a body. (Or worse, simply strip the body and handle the request without it, which would cause the server to return an incorrect result set.)

选项 2:使用非标准 HTTP 动词(可能是 QUERY)代替 GET.

Option 2: Use a non-standard HTTP verb (perhaps QUERY) instead of GET.

QUERY /namespace/collection/ HTTP/1.1
Content-Length: 22

{ /* query object */ }

虽然这并不完全符合 REST 模式,但它(对我而言)似乎是一个安全的替代方案,因为其他软件(例如任何使用 WebDAV 的软件)似乎使用非标准 HTTP 动词并取得了足够的成功.

While this doesn't fit exactly with the REST pattern, it seems (to me) like a safe alternative because other software (such as anything that uses WebDAV) seems to use non-standard HTTP verbs with sufficient success.

选项 3: 将查询对象放在非标准 HTTP 标头中.

Option 3: Put the query object in a non-standard HTTP header.

GET /namespace/collection/ HTTP/1.1
ProjectName-Query: { /* query object */ }

此选项将请求保留为 GET,但需要在 HTTP 标头中填充可能是非常大的对象.我知道有些软件对 HTTP 标头设置了任意长度限制,因此如果对象太大,这可能会导致问题.

This option keeps the request as a GET, but requires stuffing what could potentially be a very large object in an HTTP header. I understand some software places arbitrary length limits on HTTP headers, so this may cause issues if the object gets too big.

选项 4:使用 POST 动词并提供用于查询的备用端点.

Option 4: Use the POST verb and provide an alternate endpoint for querying.

POST /namespace/collection/query HTTP/1.1
Content-Length: 22

{ /* query object */ }

因为它使用标准动词并且没有标准标题,所以此方法保证适用于所有场景.唯一的问题是它偏离了 RESTful 架构,我正在尽我所能地保持一致.

Because this uses a standard verb and no standard headers, this method is guaranteed to work in all scenarios. The only issue is that it strays from RESTful architecture, which I'm trying to stay aligned with as best I can.

这些选项都不对.我想知道哪种方式对我正在编写的服务最有意义;它是一个内部 Web 服务(它永远不会向公众公开)但可以通过各种网络安全应用程序(防火墙、内容过滤器等)访问它,我想坚持已知的开发风格、标准和架构尽我所能.

None of these options are quite right. What I want to know is which way makes the most sense for the service I'm writing; it's an internal web service (it will never exposed to the public) but it may be accessed through a variety of network security applications (firewalls, content filters, etc..) and I want to stick to known development styles, standards, and architecture as best I can.

推荐答案

我会使用选项 4.很难将大型搜索请求的 json 查询表示放入 url,尤其是针对搜索服务器.我同意,在这种情况下,它不适合 Restful 风格,因为 URI 无法识别资源.REST 是一个指南.如果 REST 无法实现该场景,那么我想做一些可以解决问题的事情.在这里使用 POST 并不安静,但它似乎是正确的解决方案.

I would use Option 4. It is difficult to put the query representation in json for a large search request into an url, especially against a search server. I agree, in that case it does not fit into a Restful style since the resources cannot be identified by the URI. REST is a guideline. If the scenario cannot be realized by REST then i guess do something that solves the problem. Here using POST is not restful but it seems to be the correct solution.

这篇关于在 GET 期间是否有规范/RESTful 方式将查询详细信息发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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