Consider a RESTful service that exposes entities of type Product
whose class is declared as follows:
public class Product
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
Now suppose that we want to completely update the Product
whose ID
is 123
. Then we can easily do this by performing a standard HTTP PUT request like the following:
PUT /odata/Products(123) HTTP/1.1
Host: localhost:1337
Content-Type: application/json
{
"ID": 123,
"Name": "Shirt",
"Price": 19.99
}
What is the justification behind repeating the key twice? Shouldn't we still be able to completely update the Product
entity even if we omit the ID
key from the URL, since we could always just extract the key from the request body? I get that PUT is intended to be idempotent and that this redundancy issue could be avoided entirely by using PATCH instead of PUT, but I was just wondering about the motivation behind this weird convention. I tried reading the official protocol documentation for PUT, but they don't seem to talk about it.