I was reading some documents about the appropriate use of URI's using rest services and I came across an example for basic GET .. DELETE requests.
The example uri's were:
Get all users
GET http://mydomain.org/api/users
Get specific user
GET http://mydomain.org/api/users/1
Update user
PUT http://mydomain.org/api/users/1
DELETE user
DELETE http://mydomain.org/api/users/1
A user resource would be either JSON or XML in the form of:
{ Id: 1, FirstName: 'John', LastName: 'Doe' }
My question is this. To maintain REST principles, is it required to include the id of the resource within the URI for PUT requests?
The target URI of the PUT request should match the target URI of the GET request used to retrieve a representation of the same resource. Therefore, including an "id" in the URI for a PUT request follows precisely the same rules as including an "id" in the URI for a GET request.
HTTP verbs are preferred if possible, as they are part of the HTTP protocol and as such a standard. It also allows you to use existing security and caching layers on a standard web server, without having to write any bespoke middleware.
In RESTful HTTP the client should never construct URIs. The service should be well-connected, which means that the client should only ever follow URIs given by the server and make requests to those URIs.
A URI is used to identify a resource on the web (and other places). A RESTful API uses URIs and HTTP GET/POST/PUT/DELETE to perform CRUD (create, read, update, delete) operations on a web service. e.g.
The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.
You want to PUT
a resource to the same URI you intend to GET
it from.
RFC 72314.3.4 PUT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With