dubs
dubs

Reputation: 6521

Should a PUT request be compatible with a GET response from the same resource

I'm reviewing a number of endpoints that have been developed against the beginnings of what will be a complex API.

Are there any industry recommended best practises that say that you should ensure that a response of a GET request can be used as the payload for a PUT request against the same resource.

Additionally, I would appreciate feedback of potential pitfalls people have had taking this approach.

Upvotes: 0

Views: 184

Answers (1)

Evert
Evert

Reputation: 99533

I would say 'it helps', but it's not strictly necessary. Because content-negotiation and multiple representations are a thing, you could use a different media type for 'changing' vs 'retrieving' the state of a resource.

If the representation of your request is a simple subset, maybe PATCH is what you're looking for.

If the response of a PUT request contains an ETag it is expected that the request body of the PUT perfectly matches byte-for-byte the current state. In reality this is rarely the case though, because API's tend to re-serialize their model and will often have subtle differences.

Having that ETag there is helpful though, because a smarter client can read that ETag and use that to warm their current cached state of the resource, and also avoids a race condition if more PUT's are needed in the future and the client wants to use ETags to avoid the lost update problem.

For me personally, I like the ability to GET a resource, change a single property PUT the complete resource again.

Upvotes: 1

Related Questions