I have inherited a REST endpoint that has one key action it performs, named find. This action will generally just query for any matching resources, rank them, and return the most favorable result. However, in some cases, the payload specifies a flag indicating we should be upsert the record if no match was found. The initial build used POST, which seems wrong to me. My inclination was that find should be related to a GET call.
Upon further reading, it appears that GET should never modify server resources.
Requests using
GETshould only be used to request data (they shouldn't include data).
Also, there is currently a payload in the request body, and apparently that too is frowned upon.
It is better to just avoid sending payloads in
GETrequests.
So maybe GET is wrong, but POST still seems wrong also. It seems to me that PUT would be more appropriate.
The HTTP
PUTrequest method creates a new resource or replaces a representation of the target resource with the request payload.The difference between
PUTandPOSTis that PUT is idempotent: calling it once or several times successively has the same effect (that is no side effect), whereas successive identical POST requests may have additional effects, akin to placing an order several times.
I do believe this request would be idempotent. If you sent the same payload multiple times, the first request would create the missing resource, and subsequent requests would find it without modifying server state.
So it still seems weird to me that in the case we know we only run a query, we use anything other than GET. But requiring the caller to change their HTTP method rather than setting some flag in the request body is apparently cumbersome. I guess the question is, how much stock should I place in using the "most correct" HTTP method, and is that method in fact PUT?