By netbrain

2011-05-31 16:01:22 8 Comments

Im pretty new to REST but as far as i have gathered i understand that the following URL's conform to the REST principles. Where the resources are laid out as follows:

          ^         ^           ^   ^
          |---------|-----------|---|- user resource with username as a variable
                    |-----------|---|- many to one collection (books)
                                |---|- book id 
                                    |- many to one collection (tags)

GET /user/dave/library/book             //retrieves a list of books id's
GET /user/dave/library/book/1           //retrieves info on book id=1
GET /user/dave/library/book/1/tags      //retrieves tags collection (book id=1)

However, how would one go about optimizing this example API? Say for example i have 10K books in my library and i want to fetch the details of every book in my library. should i really force a http call to /library/book/<id> for every id given in /library/book? Or should i enable multiple id's as parameters? /library/book/<id1>,<id2>... and do like bulk fetching with a 100 id's at a time?

What does the REST principles say about this kind of situation? and what are your opinion(s)?

Thanks again.


@sergio 2011-05-31 16:07:59

This is strictly a design matter.

I could define a bookc resource and use it like this:

GET /user/dave/library/book?bookList=...

how do you further specify the bookList argument is really a matter of what kind of usage you envisage of this resource. You could have, e.g.:

GET /user/dave/library/book?bookList=1-10
GET /user/dave/library/book?bookList=1,2,5,20-25

or you could simply page through all of the books:

GET /user/dave/library/book?page=7&pagesize=50

But in my mind, especially the form with a long list of "random" ids seems pretty unfit. Maybe I would instead define a filter parameter so I can specify:

GET /user/dave/library/book?filter=key,value&filter=key,value

As to your question about HTTP URL length limit, the standard does not set any. But browser may vary... look at this S.O. topic

To be more strictly RESTful, the query parameter could be specified through HTTP headers, but the general idea I wanted to convey does not change.

Hope this seems suitable to you...

@sergio 2011-05-31 16:14:07

check also this s.o. topic:…

@netbrain 2011-05-31 17:10:17

how would you format the Id's? comma separated? and what is the limit on how much data you can put on query string? would it be better (allowed?) to use post and post parameters instead?

@Brian Kelly 2011-05-31 17:13:02

I would avoid specifying the query parameter in a HTTP header because that would mean (a) that you could have multiple queries which are completely different but which share the same URI, and (b) it would remove the ability for intermediate proxies to cache query results to improve performance.

@java_geek 2014-10-06 05:11:33

I would rather use the following URL's for your resources /users/dave/books/

@Wilt 2014-04-30 16:08:31

You can use a paginator

Some restful API's work with a paginator for huge resources like:

The server delivers for example 100 records (in this case books) per page. And you can sort the books using a sortby in your get request. With the above request you would get books 101-200 (if so many in the database). The response can tell you something about the amount of books and amount of pages, what is the next page and the previous page but then you go more to HATEOAS.

Otherwise if you want to get certain id's i would do it like this:[]2&id=[]5&id=[]7&id=[]21

A get request with an array of id's (id = [2,5,7,21]) which returns the books with those respective id's

@manuel aldana 2011-05-31 20:08:18

Above looks good, but I would change to plural names, it reads better:


What I don't understand is the use-case of passing comma-separated list of ids. The question is how you get to the ids? I guess behind the list of ids there are semantics, i.e. they represent a result of a filter. So instead of passing ids I would go for a search api. Simplistic example:


If you want to iterate through your 10K collection of books, use paging parameters.

@Volkmar Rigo 2011-05-31 16:08:17

this is just my opinion:

GET /user/dave/library/book/IDList      //retrieves a list of books id's
GET /user/dave/library/bookID           //retrieves a list of books id's

GET /user/dave/library/book             //retrieves a list of books
GET /user/dave/library/book/1           //retrieves info on book id=1
GET /user/dave/library/book/1-3         //retrieves info on book id>=1 and id <=3
GET /user/dave/library/book/1/tags      //retrieves tags collection (book id=1)

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] Bulk Collection Manipulation through a REST (RESTful) API

  • 2008-11-20 18:08:01
  • mattwynne
  • 20982 View
  • 72 Score
  • 7 Answer
  • Tags:   http rest

7 Answered Questions

[SOLVED] How to handle many-to-many relationships in a RESTful API?

16 Answered Questions

1 Answered Questions

How to implement a RESTful API for order changes on large collection entries?

3 Answered Questions

[SOLVED] RESTful API design - how to handle foreign keys?

  • 2015-11-26 12:24:55
  • Geoffrey De Vylder
  • 5059 View
  • 8 Score
  • 3 Answer
  • Tags:   rest

3 Answered Questions

[SOLVED] REST - HTTP DELETE - semantics - only delete descendants

1 Answered Questions

[SOLVED] How to represent collections in HAL (JSON version) while following REST

4 Answered Questions

[SOLVED] How should I deal with object hierarchies in a RESTful API?

Sponsored Content