Proper REST formatted URL with date ranges

后端 未结 3 854
太阳男子
太阳男子 2020-12-24 00:27

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 g

相关标签:
3条回答
  • 2020-12-24 00:51

    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.
    0 讨论(0)
  • 2020-12-24 00:56

    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.

    0 讨论(0)
  • 2020-12-24 01:01

    if example.com/users/12345 gets the user with id 12345, then to get all users by id it should be example.com/users with the id included in the response as a relationship. (usually a hyperlink to that resource).

    Now to get them by date ranges it should be example.com/users/start=01-01-2012&end=01-31-2012

    The 12345 part is the id of an individual user, it's a resource, therefore it should not be included to get the rest of the users.

    As the name of the parameter it should be meaningful. start could mean anything, but start_date is more meaninful.

    0 讨论(0)
提交回复
热议问题