What is the most common/industry standard response code for HTTP PUT when:
In my specific case the client sends HTTP PUT with an empty JSON like this:
{}
while I expect something more like this:
{
key1: {
something: value,
something2:value2
},
key2: {
something: value3,
something: value4
}
}
which in my case would translate to 4 new rows being upserted to the database.
I am considering either 400 (because maybe it is a bad request when you call a HTTP PUT but you don't have anything to put there), 200, 204 and 304.
My question is different from this and this because they are about HTTP GET method, and is different from this because while it is about HTTP PUT the answer doesn't address my case.
A PUT
request should replace what was on the server at the specified uri.
So if the resource at the uri already was empty, and the new resource that's being put is also empty, nothing changed but it's still a success, so 200 OK
is fine.
If the resource didn't exist and you create a new 0-byte resource, 201 Created
might be more appropriate.
If the resource did exist but it was not empty, and you send an empty PUT
request, it should replace the existing resource with an empty one.
This is where you are going wrong, because PUT
should not be used for a per-record "upsert" like you are doing.
If the PUT request you state was semantically correct, it should wipe out all existing records tied to that location.
What you probably want is PATCH
or POST
.