3

Let's say I have three resources that are related like so:

Grandparent (collection) -> Parent (collection) -> and Child (collection)

The above depicts the relationship among these resources like so: Each grandparent can map to one or several parents. Each parent can map to one or several children. I want the ability to support searching against the child resource but with the filter criteria:

If my clients pass me an id reference to a grandparent, I want to only search against children who are direct descendants of that grandparent.

If my clients pass me an id reference to a parent, I want to only search against children who are direct descendants of my parent.

I have thought of something like so:

GET /myservice/api/v1/grandparents/{grandparentID}/parents/children?search={text}

and

GET /myservice/api/v1/parents/{parentID}/children?search={text}

for the above requirements, respectively.

But I could also do something like this:

GET /myservice/api/v1/children?search={text}&grandparentID={id}&parentID=${id}

In this design, I could allow my client to pass me one or the other in the query string: either grandparentID or parentID, but not both.

My questions are:

1) Which API design is more RESTful, and why? Semantically, they mean and behave the same way. The last resource in the URI is "children", effectively implying that the client is operating on the children resource.

2) What are the pros and cons to each in terms of understandability from a client's perspective, and maintainability from the designer's perspective.

3) What are query strings really used for, besides "filtering" on your resource? If you go with the first approach, the filter parameter is embedded in the URI itself as a path parameter instead of a query string parameter.

Thanks!

HiChews123
  • 1,448
  • 3
  • 20
  • 38
  • 1
    If you didn't already see this, maybe this could help you choose: http://stackoverflow.com/questions/4024271/rest-api-best-practices-where-to-put-parameters – Bogdan Jan 24 '15 at 14:47
  • https://softwareengineering.stackexchange.com/questions/270898/designing-a-rest-api-by-uri-vs-query-string – whoan Nov 05 '17 at 18:24

1 Answers1

0

Something to note, is that since you use GET in your above examples, dependant on the end user browser settings, proxy settings or other calling applications internal settings, the response is likely to be cached somewhere on the message route, and the original request message may never actually reach your API layer where the most recent data is accessed. so first, you may want to review your requirement of using the verb GET. If you want your API to return the most recent data every time, then don't use GET, or if you still want to use GET, then require the caller to append a random number to the end of the url, to decrease the likely hood of caching. or get the client to send the verb PURGE, after every GET.

this proxy caching behaviour that is present across the internet, in browsers, and server architectures is where REST fails for me, since caching of GET can be very useful but only sometimes. stick to basic querystrings if you want to make it really simple for the end developer and caching of responses offers no problems for you, or just use POST and forget about this tiresome REST argument

Sean Bradley
  • 1,980
  • 1
  • 12
  • 9