Currently developing a REST API, with a suite of endpoint functions which update the "state" of a particular resource.
I am using POST to create the initial resource and then updating the state using PUT - Is PUT the correct method to be using?
The state updates are being logged in a journal, so to avoid someone updating the state with the same value multiple times, I wish to put some business logic in that avoid two repeat entries of the same state. If someone attempts to call the same function twice, lets say "CancelResource()" - should I return a 200 success on the second call, and just not make an update, or would it be better to send some sort of error response?
I was considering returning a 405 "method not allowed" but this feels a little harsh. I also don't know that 200 would be very useful for the client.
PUTis appropriate for updates, andPUTshould be idempotent. This means that if you do the exact samePUTrequest twice in a row, the server state should be no different from if you only did it once.This is useful in for example unreliable connections. Clients can just repeat the request if it failed, without having to worry that the server somehow did receive the request and process it.
So if you have a means to detect identical requests, ignoring the second and return a
200 OKon both that's an excellent way to handle this.