设计REST风格的查询API与查询参数一长串 [英] Design RESTful query API with a long list of query parameters

查看:203
本文介绍了设计REST风格的查询API与查询参数一长串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要设计一个RESTful查询API,它返回一组基于一些过滤器对象。这个通常的HTTP方法是GET。唯一的问题是,它可以有至少有十几个过滤器,如果我们通过它们作为查询参数的URL可以得到相当长(长到足以被一些防火墙阻止)。

So, I need to design a RESTful query API, that returns a set of objects based on a few filters. The usual HTTP method for this is GET. The only problem is, it can have at least a dozen filters, and if we pass all of them as query parameters, the URL can get quite long (long enough to be blocked by some firewall).

减少的参数的数目是不是一种选择。

Reducing the numbers of parameters is not an option.

一个选择,我能想到的是利用对URI POST方法和发送过滤器作为POST身体的一部分。这是对被REST风格(拨打POST调用查询数据)。

One alternative I could think of is to make use of the POST method on the URI and send the filters as part of the POST body. Is this against being RESTfull (Making a POST call to query data).

任何人有任何更好的设计建议?

Anyone have any better design suggestions?

感谢

推荐答案

很多人已经接受的做法,与太长或太复杂的查询字符串GET(如查询字符串不容易处理嵌套数据)与复杂的/长数据重新请求的身体psented $ p $发送一个POST代替。

A lot of people have accepted the practice that a GET with too long or too complex a query string (e.g. query strings don't handle nested data easily) can be sent as a POST instead, with the complex/long data represented in the body of the request.

查找了POST规范中的HTTP规范。这是令人难以置信的宽广。 (如果你想通过REST的漏洞帆战舰......使用POST)。

Look up the spec for POST in the HTTP spec. It's incredibly broad. (If you want to sail a battleship through a loophole in REST... use POST.)

您失去一些GET语义的待遇,如自动重试,因为GET是幂等的,但如果你可以忍受的,它可能是更容易只是接受处理很长的或复杂的查询与POST。

You lose some of the benefits of the GET semantics ... like automatic retries because GET is idempotent, but if you can live with that, it might be easier to just accept processing really long or complicated queries with POST.

(笑长题外话......我最近发现,通过HTTP规范,GET的可以的包含文档的身体。还有一个部分,上面写着,释义,任何请求都可以有一个文档全身除在本节...它是指没有列出任何一节中列出的人。我搜索,发现那里的HTTP作家都在谈论一个线程,这是故意的,让路由器和这样不会有不同的消息之间进行区分。然而,在实践中很多基础件做砸一个GET的身体。所以,你可以带过滤器重新在体内,如POST psented $ p $ GET,但你会滚动骰子。)

(lol long digression... I recently discovered that by the HTTP spec, GET can contain a document body. There's one section that says, paraphrasing, "Any request can have a document body except the ones listed in this section"... and the section it refers to doesn't list any. I searched and found a thread where the HTTP authors were talking about that, and it was intentional, so that routers and such wouldn't have to differentiate between different messages. However, in practice a lot of infrastructure pieces do drop the body of a GET. So you could GET with with filters represented in the body, like POST, but you'd be rolling the dice.)

这篇关于设计REST风格的查询API与查询参数一长串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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