如何为HTTP GET的多个Key-Value参数设计REST URI [英] How to design REST URI for multiple Key-Value params of HTTP GET

查看:381
本文介绍了如何为HTTP GET的多个Key-Value参数设计REST URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个RESTful API。

I am designing a RESTful API.

一项服务应该为多个键值对提供查询功能。例如,客户端可以使用一个HTTP GET请求查询不同的产品和相关数量。

One service should offer query functionality for multiple key-value pairs. For example, the client can query with one HTTP GET request for different products and the associated quantity.

客户端想要使用金额44和产品2查询产品1金额55.
我实际上不希望我的URI看起来像这样:

The client wants to query product 1 with the amount 44 AND product 2 with the amount 55. I actually don't want my URI to look like this:

/produkt?productId1=1&productquantity1=44&productId2=2&productquantity2=55

因为我不知道有多少产品被查询。

because I don't know how many products are queried.

或者像这样:

/produkt?product=1,44&product=2,55

因为客户怎么能在逗号之前知道有产品ID和逗号之后的数量。

because how can the client know that before the comma there is the productId and after the comma the quantity.

有没有人有其他解决方案?或者,提供使用一个请求查询多个产品的可能性是不是RESTful?提供查询一个产品的可能性是否更好,如果客户想查询更多产品,他们应该发送更多请求?

Does anyone have other solutions? Or is it not RESTful to offer the possibility to query multiple products with one request? Is it better to offer the possibility to query just one product with the associated quantity and if the client wants to query more products, they should send more requests?

推荐答案

我会稍微扩展您的第二个建议,为产品的各个部分添加明确的名称,并使用分号代替逗号分隔产品属性,因为顺序无关紧要 *

I would expand upon your second suggestion a little by adding explicit names for the parts of your product, and using semicolons in place of commas to separate product attributes, since the order does not matter*.

/products?id=1;qty=44&qty=55;id=2

注意 id qty 为第二个产品切换,因为属性的顺序无关紧要。

Note how id and qty are switched around for the second product, because the order of attributes does not matter.



* 当订单很重要时有一个使用逗号的约定,当订单不重要时使用分号。


* There is a convention to use commas when the order is important, and semicolons when the order is not important.

这篇关于如何为HTTP GET的多个Key-Value参数设计REST URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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