在 REST API 中使用 的合适用例是什么?参数问号?

What is the appropriate use case in a REST API to use the ? Question Mark for parameters?

this REST 教程网站上,

什么时候(如果有的话)放像

这样的东西合适
http://dev.m.gatech.edu/developer/USER_NAME/api/WIDGET_NAME/test?query=someparam

而不是

http://dev.m.gatech.edu/developer/USER_NAME/api/WIDGET_NAME/test/someparam

http://dev.m.gatech.edu/developer/USER_NAME/api/WIDGET_NAME/test/someparam/var1/param/var2/param

?

我在 SO 上看到了很多东西。

所有执行 GET 请求并需要传递一些参数的情况都应该采用 ?param=value 的形式。

So is that first link up top, they are just wrong? Man, who can you trust these days :).

不,他们没有错。从那个网站拿这个例子

GET http://www.example.com/customers/33245/orders

这里,customers33245orders不是查询参数,它们是资源端点,或者uri节点,因为它们调用他们在你的 restapitutorial.com

如果你这样做

GET http://www.example.com/customers 你得到了所有客户
GET http://www.example.com/customers/33245 你得到客户 33245
GET http://www.example.com/customers/33245/orders 您获得客户 33245 的订单

它们都是return 0 个或更多资源。例如,如果您要对第一个查询应用查询,并且您想获取所有名字为 John 的客户,您可以这样做

GET http://www.example.com/customers?firstname=John

在你问题的最后一个例子中,它会被写成 GET http://www.example.com/customers/firstname/john 而不是,这在宁静方面是 错误的 。没有客户资源 'firstname',也没有名字资源 'john'。

有些客户的名字是 'john',您可以用

来获取他们
GET http://www.example.com/customers?firstname=John