Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST URL design - multiple resources in one HTTP call [duplicate]

Tags:

rest

http

api

Possible Duplicate:
Rails 3 Custom Route that takes multiple ids as a parameter

From what I understand, a good REST URL for getting a resource would look like this:

/resource/{id}

The problem I have is, that I often need to get a large number of resources at the same time and do not want to make a separate HTTP call for each one of them.

Is there a neat URL design that would cater for that or is this just not suitable for a REST API?

like image 600
George Avatar asked Jun 09 '09 11:06

George


3 Answers

Based on your response, the answer to your question is to create a new resource that contains that single set of information. e.g.

GET /Customer/1212/RecentPurchases

Creating composite urls that have many identifiers in a single url limits the benefits of caches and adds unnecessary complexity to the server and client. When you load a web page that has a bunch of graphics, you don't see

GET /MyPage/image1.jpg;image2.jpg;image3.jpg

It just isn't worth the hassle.

like image 114
Darrel Miller Avatar answered Nov 06 '22 00:11

Darrel Miller


I'd say /resources/foo,bar,baz (separator may vary depending on IDs' nature and your aesthetic preferences, "foo+bar+baz", "foo:bar:baz", etc.). Looks a bit "semantically" neater than foo/bar/baz ("baz of bar of foo"?)

If resource IDs are numeric, maybe, even with a range shortcut like /resources/1,3,5-9,12

Or, if you need to query not exactly on resources with specifical IDs, but on group of resources having specific properties, maybe something like /resources/state=complete/size>1GiB/!active/...

like image 26
drdaeman Avatar answered Nov 05 '22 22:11

drdaeman


I ahve used in the past something like this.

/resources/a/d/

and that would return between x and Y a list.

something like

<resources>
  <resource>a</resource>
  <resource>b</resource>
  <resource>c</resource>
  <resource>d</resource>
</resources>

you could also put more advanced searches into the URL dpending on what resource actuall is.

like image 1
Bluephlame Avatar answered Nov 05 '22 22:11

Bluephlame