Suppose you are working on an API, and you want nice URLs. For example, you want to provide the ability to query articles based on author, perhaps with sorting.
Standard:
GET http://example.com/articles.php?author=5&sort=desc
I imagine a RESTful way of doing this might be:
GET http://example.com/articles/all/author/5/sort/desc
Am I correct? Or have I got this REST thing all wrong?
You are mostly right. The thing with REST api’s is to focus on the nouns.
What does the noun
alldo in this case? Wouldn’t you expect your API to always return all articles, unless you filter it?I would make
sorta query string parameters, further, I would make any and all filtering query string parameters. If you look at how Stack is implemented when you click on the “Newest” questions link, you get a query string to filter the questions.So perhaps something like:
GET http://example.com/aritcles/authors/5?sort=descBut also think about what happens with each URL:
GET http://example.com/aritcles/might return all current articlesGET http://example.com/aritcles/authors/What does this url do? does it return all authors of all articles, or does it return all the articles for all authors (which is essentially the same functionality of the URL above.)GET http://example.com/aritcles/authors/5/might return all articles by author 5, or does it return author 5’s information?I would maybe change it to:
http://example.com/aritclesreturns all articleshttp://example.com/aritcles/5returns all articles from author 5http://example.com/authorsreturns all authorshttp://example.com/authors/5returns information for author 5