Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper REST formatted URL with date ranges

Tags:

rest

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

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

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

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

or should it be like this: http://example.com/users/12345/bids/start/01012012/end/01312012

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?

Thanks,

Tom

like image 620
Thomas Kessler Avatar asked Mar 09 '12 16:03

Thomas Kessler


People also ask

How do you pass date range in rest URL?

What is the proper way to format a REST URL with a date range? I would suggest putting the dates in ISO 8061 format: 2012-01-31. This avoids uncertainty about whether it's month-day-year or day-month-year.

How do you send a date in a URL?

The ISO 8601 standard is yyyy-MM-dd, which is unambiguous and does not contain any invalid URL characters, and works fine in DateTime. Parse/TryParse. Another option is to use whatever format you want and simply encode the URL using HttpServerUtility. UrlEncode/UrlDecode.


2 Answers

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.

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.

like image 111
smcg Avatar answered Oct 04 '22 12:10

smcg


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

  • There shouldn't be a slash before the query string.
  • Avoid using slashes in the query string. It'll be easier that way.
like image 15
whirlwin Avatar answered Oct 04 '22 13:10

whirlwin