Nick Gotch
Nick Gotch

Reputation: 9407

How to handle a mismatch between the PUT body and URI in a REST update?

If a PUT request is sent to a URI such as http://foo.com/api/employees/123 where '123' is the employee ID, but the message body looks like this:

{
  "id" : 444,
  "firstname" : "John",
  "lastname" : "Doe"
}

How should the service handle it?

Is the expectation to update employee '123' to now become employee '444' (effectively moving the resource) or should it return an HTTP error response (and if so, what should it be?)

Upvotes: 1

Views: 421

Answers (2)

Thierry Templier
Thierry Templier

Reputation: 202138

I think that the id within the URI should be used and no id present in the message body. You have duplicated hints here.

The content you provide within the PUT method contains the data you want to update and it's not the case of the identifier.

Here is how I would adapt your call:

PUT http://foo.com/api/employees/123
(some headers like Content-Type: application/json)
{
  "firstname" : "John",
  "lastname" : "Doe"
}

Perhaps this link could help you: http://templth.wordpress.com/2014/12/15/designing-a-web-api/.

Hope it helps. Thierry

Upvotes: 1

Ali Kemal Saglam
Ali Kemal Saglam

Reputation: 66

You might use this Id according what you want to do into your put resource. Usually, id in resource uri is used to get item by id from persistence db. You can get item by uri id then you can set this id into body, but this will be odd!

Upvotes: 0

Related Questions