Synchronizing data once user gets online involves both Insert
and Update
(Upsert
) and I'm sending both kinds of records in a single request (array
) and then server iterates through records to determine insert or update.
My question is whether to use POST
or PUT
?
Also how a response from the server (JSON) should like in it's body? the data sent is an array, for example
{
"ids" : "15,16,17",
"success" : true
}
Edit:
And what should be the response code, it has both create and update operations:
200 OK
201 Created
REST is not CRUD. Mapping HTTP methods to CRUD operations is a convention introduced by some frameworks, but it has nothing to do with REST. Read this answer for some clarification on that.
A PUT
is a complete replacement that ignores the current state of the resource. Think of the mv
command in a shell. If there's nothing on the destination, it creates it. If there's something, it replaces completely, ignoring whatever is in there. That's how a PUT
should work. Ideally, your application should have an uniform implementation of PUT
that works in the exact same way with any URI that supports the method..
A POST
submits the payload to be processed by the target resource under predefined rules. This means you can use POST
for any operation that isn't already standardized by the HTTP protocol.
In your case, it's clearly not a complete replacement, so it's not a case for PUT
. Use POST
.