Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are correct that the former URL is "better" and is more widely deployed. I don't think you would find this documented in any <em>standard</em>; it is rather more of a convention. Most articles and books covering REST do it that way.</p> <p>The reason for this is, as you say, that the path components in the URL match the structure of resources and sub-resources. In particular, all of the following should be valid URLs:</p> <ul> <li>website.com/</li> <li>website.com/books</li> <li>website.com/books/123</li> </ul> <p>In particular, note that it is <code>books/123</code>, <strong>not</strong> <code>book/123</code> like you have. I have seen the singular but IMHO the plural is better.</p> <p>For the URL <code>/books</code></p> <ul> <li>a GET gets all books, but you can restrict the books with query parameters, e.g. <code>/books?author=alice</code></li> <li>a POST adds a new book (with a server-generated id).</li> </ul> <p>For the URL <code>/books/123</code></p> <ul> <li>a GET gets that particular book</li> <li>a PUT replaces the book with that id (or adds a book with that client-generated id)</li> </ul> <p>Now if a book has blurbs and the blurbs are unique <em>only to a particular book</em> then you will add the following URLs:</p> <ul> <li>website.com/books/123/blurbs</li> <li>website.com/books/123/blurbs/72</li> </ul> <p>You can do the same for films and galleries, provided each gallery belonged to a single film. But if galleries existed for multiple films, then you would make <code>/galleries</code> a top-level URL. Navigating from a film to a gallery would still be fine. You wouldn't have a structured URL. You would instead get all galleries containing pictures from film 456 via a GET to</p> <ul> <li>website.com/galleries?film=456</li> </ul> <p>The general rule is that if you have an ownership relation for the subresources you can use structured urls, but if there is a looser relationship among top-level items, query parameters are fine. Don't fall into the common misconception that RESTful URLs don't have query parameters; they do. :)</p> <p>Now finally, to directly answer your question: <code>website.com/films/galleries/456</code> is not a good URL IMHO because <code>`website.com/films/galleries/</code> is not very useful. In fact I think it is rather ugly. What would it mean? All galleries? If so, it should be <code>website.com/galleries</code>.</p> <p>Again I don't think this is standardized anywhere, but it feels very common and conventional.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload