tags:

views:

98

answers:

4

I had a discussion with a colleage today around using querystring in REST urls. take these 2 examples

1. http://localhost/findbyproductcode/4xxheua
2. http://localhost/findbyproductcode?productcode=4xxheua

My stance was the urls should be designed as example 1. This is cleaner and what I think is correct within rest. In my eyes you would be completely correct to return a 404 error from example 1 if the product code did not exist where as with example 2 returning a 404 would be wrong as the page should exist. His stance was it didnt really matter and that they both do the same thing.

As nether of us where able to find concreate evidance (admitably my search was not extensive) I would like to know other peoples opinions on this.

Kind Regards Colin G

+3  A: 

In typical REST API's, 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/tagged/rest
stackoverflow.com/questions/3821663

These are all different ways of getting at "questions".

Steve Michelotti
A: 

I agree with Steve - option 1 is better. If option 2 returns a 404 (which is, without doubt, the correct response for an unknown product), what does that mean? Does it mean that the product is not found, or does it mean that the resource http://localhost/findbyproductcode is missing?

Steve Strong
It cannot mean the latter, since the resource id identified by the whole URI, not just the 'query' portion (as darrel mentioned above).
fumanchu
+4  A: 

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 Steve's comment.

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

I think the answer to whether to return 404 depends on the what is the resource 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 shouldn't 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 don't want to return 404s.

Darrel Miller
Quoting the RFC spec is fine but that's not exactly the question being asked. Yes, the two examples are functionally equivalent - that is not in dispute. The question goes beyond the textbook "definition" of a resource (for which they both apply). To his question, what to happen if code in query string isn't there? 404? What about the "cleaner" aspect of his question? Both are "valid", yes, but IMHO, #1 is "cleaner" and more in line with what he is seeking (in conjunction with my answer below with StackOverflow).
Steve Michelotti
@Steve See updated answer.
Darrel Miller
@Darrel - I agree with the comparison you gave in your updated answer. query string makes sense for a search result with no 404s. For a product code (as per this question) 404 makes sense and IMO it's more common to not use query string for this scenario. Thanks for the updated answer.
Steve Michelotti
A: 

The ending of those two URIs is not very significant RESTfully.

However, the 'findbyproductcode' portion could certainly be more restful. Why not just http://localhost/product/4xxheau ?

In my limited experience, if you have a unique identifier then it would look clean to construct the URI like .../product/{id} However, if product code is not unique, then I might design it more like #2.

However, as Darrel has observed, the client should not care what the URI looks like.

pc1oad1etter