Let's say I'm building a note-taking app and I periodically sync the note's content with the server. If there is a diff, then the content is stored, else ignored.
What would be the ideal status code to send back if there is no diff and no operation is performed? 🙂
Many replies mentioned 304. It might be worth than 304 is intended to help the client manage the local state (e.g. cache) of a resource.
Since you're talking about sync, it all depends if your request is getting the trying to get the data (sync from server to client) or pushing some data to the server (which then respond telling you nothing changed). The former use case is a valid 304. The later is not.
Furthermore, 304 is OK if the request is a GET or HEAD, and the server returns a cache-control related header like Cache-Control, Content-Location, Date, ETag, Expires, and Vary. Otherwise, intermediate layers (proxies, ...) could not correctly deal with that request (remember 304 are really targeted at managing cache)
Ref (and links to RFCs): https://httpstatuses.com/304
The correct answer to your question really depends on the logic of your sync:
304 is perfect for no change, otherwise it's a 200 to which your client sends the data to perform the sync201 for the very first sync, letting know the client this is a new resource (which might triggers some additional validation/local ref storage)200 any other successful time, with a few fields in the body: the diff, and eventually some meta-data (depending on the business logic and use cases. Maybe a csrf token too, that kind of decorating data not directly related to the content of the sync)409 is it's a no-op because the operation cannot be performed (users connected from 2 different clients, both trying to sync and ending with a merge conflict for the later oneFinally, whenever I wonder which one I should use, I very often coming back to these 2 resources:
Hope this helps
200 OK would work 👍
Or try with 201 Created for when a new resource was created and 204 No Content when all went ok but nothing was returned.
304 Not Modified is not because it needs some request headers: If-Modified-Since , If-None-Match . I think this is more for GET requests.
I use these following codes frequently :
For Reading data from Server : 200 OK
NO Content Found : 204 No Content
Data Insertion Operation : 201 Created
No Operation : 304 Not Modified
you can use 304 as not modified
if you have any doubt in status code just check here or feel free to ask
Cosmin Albulescu
Software Engineer
What about
200 OKand returning a short text specifying the status of synchronization?