Including Id in URI for PUT requests
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 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
I was going to ask a similar question, but I think I found the answer. I am not sure if it is by REST principles, but here is why it would be bad not to include ID in the URI.
So say your PUT
is like:
PUT http://mydomain.org/api/users
And then you happen to update multiple users with different id's but the same URI cause there is no ID in your URI. Then, an important thing to know here is that PUT is idempotent http verb. This means that calling it once should have the same effect as calling it mulitple times. Therefore, some intermediate node in the network, just following the fact that you PUT multiple times, might ignore all but one of your requests cause they have the same URI. Finally, that's definetely not what you want cause the intention was to update multiple users not only one.