Simple reference to URL pattern in RESTful methodology
Guidelines for RESTful URLs
Finding restful rules between REST clients and servers was more time consuming than other searches. It must have been a block for others, too as I saw the question posted on various groups. So I thought maybe it made sense to post my results here.
Books as an example seems common, so I use books in my sample below.
References:
- My instructor
- This reference. While under a Ruby site, it applies to other languages:
- https://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
- This reference was very useful, but it only goes what I call "one deep" that means in their example of "photos" we can act on photos/collection of photos. However, it does not teach us how to add other "things" to photos. For example, if the application is about users having photos, then we would add photos to a user. Or, if about the various users who have a certain photo, then we would be adding photos to that user
- A Tutorial I found.
- https://restfulapi.net/resource-naming/
- Note that he says URIs, but I say URLs.
- All URLs are URIs:
- He has some opinions in his document...as noted by the commenters. That is okay. Determining URLs (and routes) seems to have wiggle room. I like his point about consistency
- Various sources on these methods and when they should and should not be used:
- GET
- POST
- PUT
- DELETE
- Note: Opinions vary on implementation of these, too.
- Idempotency
Here is a sample set of URLs for the case where a certain book (same ISBN not same physical copy, of course) can be owned by many people, and a single person can own many books.
- /books
- GET: shows/delivers all books
- GET with suffix as below renders a page for creation of a new book (used with html views)
- /books/new
- POST: creates a new book
- /books/2 Note: 2 is just an example...could be any integer
- GET: renders/delivers book number 2
- with suffix edit renders page for editing as in:
- /books/2/edit
- POST: creates book number 2
- PUT: edits or updates book number 2
- Delete: deletes book number 2
- /books/2/users/opt#3 note that in the html case, opt#3 may be handled by session, but for independent client exchange (simulated by Postman) it must be supplied
- GET: ?? I suppose that could be about showing/delivering the case of book#2 owned by user 3
- POST: adds user3 to book 2
- PUT: ?? modifies something...I don't think this would be used..unless there is something unique for this relationship between this book and this user...like maybe the due back date that could be changed
- DELETE: deletes user 3 from book 2...or user 3 returns or disposes of book
Some of the rules used as basis for above:
- GET should never alter data. Further to quote one of my sources listed above:
- GET, HEAD, OPTIONS and TRACE methods are defined as safe, meaning they are only intended for retrieving data. This makes them idempotent as well since multiple, identical requests will behave the same.
- Note the definition of idempotent:
- From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request. Note that while idempotent operations produce the same result on the server (no side effects), the response itself may not be the same (e.g. a resource's state may change between requests).
- PUT is used for modifications
- Only POST can really be used for non-idempotent cases. For example POST always gives something different
The above is some of what I learned and a reference.
Comments
Post a Comment