Querystring in REST Resource url

Querystring in REST Resource url

There is no difference between the two URIs from the perspective of the client. URIs are opaque to the client. Use whichever maps more cleanly into your server side infrastructure.

As far as REST is concerned there is absolutely no difference. I believe the reason why so many people do believe that it is only the path component that identifies the resource is because of the following line in RFC 2396

The query component is a string of
information to be interpreted by the
resource.

This line was later changed in RFC 3986 to be:

The query component contains
non-hierarchical data that, along with
data in the path component (Section
3.3), serves to identify a resource

IMHO this means both query string and path segment are functionally equivalent when it comes to identifying a resource.


Update to address Steves comment.

Forgive me if I object to the adjective cleaner. It is just way too subjective. You do have a point though that I missed a significant part of the question.

I think the answer to whether to return 404 depends on what the resource is that is being retrieved. Is it a representation of a search result, or is it a representation of a product? To know this you really need to look at the link relation that led us to the URL.

If the URL is supposed to return a Product representation then a 404 should be returned if the code does not exist. If the URL returns a search result then it shouldnt return a 404.

The end result is that what the URL looks like is not the determining factor. Having said that, it is convention that query strings are used to return search results so it is more intuitive to use that style of URL when you dont want to return 404s.

In typical REST APIs, example #1 is more correct. Resources are represented as URI and #1 does that more. Returning a 404 when the product code is not found is absolutely the correct behavior. Having said that, I would modify #1 slightly to be a little more expressive like this:

http://localhost/products/code/4xheaua

Look at other well-designed REST APIs – for example, look at StackOverflow. You have:

stackoverflow.com/questions
stackoverflow.com/questions/tagged/rest
stackoverflow.com/questions/3821663

These are all different ways of getting at questions.

Querystring in REST Resource url

There are two use cases for GET

  1. Get a uniquely identified resource
  2. Search for resource(s) based on given criteria

Use Case 1 Example:

/products/4xxheua
Get a uniquely identified product, returns 404 if not found.

Use Case 2 Example:

/products?size=large&color=red
Search for a product, returns list of matching products (0 to many).

If we look at say the Google Maps API we can see they use a query string for search.

e.g.
http://maps.googleapis.com/maps/api/geocode/json?address=los+angeles,+ca&sensor=false

So both styles are valid for their own use cases.

Leave a Reply

Your email address will not be published. Required fields are marked *