带有日期范围的正确 REST 格式的 URL [英] Proper REST formatted URL with date ranges

查看:43
本文介绍了带有日期范围的正确 REST 格式的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST URL 来获取所有用户的格式如下:http://example.com/users

I have a REST URL to get all users formatted like this: http://example.com/users

通过 id 获取单个用户:http://example.com/users/12345

To get an individual user by id: http://example.com/users/12345

要获取所有用户的出价:http://example.com/users/12345/bids

To get all user's bids: http://example.com/users/12345/bids

要获取两个日期之间所有用户的出价:http://example.com/users/12345/bids/?start=01/01/2012&end=01/31/2012

To get all user's bids between two dates: http://example.com/users/12345/bids/?start=01/01/2012&end=01/31/2012

或者应该是这样的:http://example.com/users/12345/bids/start/01012012/end/01312012

我倾向于第一个日期范围 URL,因为开始和结束不是域中的实体.使用日期范围格式化 REST URL 的正确方法是什么?

I'm leaning towards the 1st date range URL as start and end are not entities in the domain. What is the proper way to format a REST URL with a date range?

谢谢,

汤姆

推荐答案

http://example.com/users/12345/bids?start=01-01-2012&end=01-31-2012

使查询参数与出价处于同一级别"(删除问号前的斜线).但是,如果它们只提供一个查询参数,您可能希望获得支持.因此,如果他们仅提供开始",那么它将获得该日期之后的所有出价,或者如果他们仅提供结束",则将获得该日期之前的所有出价.

Have the query parameters on the same "level" as the bids (remove the slash before the question mark). But you would probably want to have support for if they only provide one query parameter. So if they only provided "start" then it would get all bids after that date, or if they only provided "end" it would get all bids before that date.

原因是查询参数适用于从 GET 请求中获取结果的子集.它们不会再上一层,因为下一层通常是具有唯一标识符的特定项目.

The reasoning being that query parameters are good for GETting a subset of results from a GET request. They don't go on another level because the next level is usually one specific item with a unique identifier.

这篇关于带有日期范围的正确 REST 格式的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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