Rest – Web API POST: single item vs collection

restweb-api

No code to show (and not really a code issue) but I have an iot-ish application running that is using PI Zeroes as clients and they are slow. A single POST takes about 10 seconds round trip, the delay seems to be almost entirely within the client side Python script, possibly related to slow connections but I would like to POST/log more than 1 entity every 10 seconds.

I think about POSTing an array or other collection of values which should be more efficient but I am not sure that is very "RESTful". What do I return? A single status code across all entities? Is there a different HTTP Verb that I should consider? The client will never use the new IDs on the POSTed entities or requery them so that isn't an issue.

Bottom line is it reasonable to POST multiple models and then return a collection of the multiple resulting entities each with their own IDs? Better to POST arrays or embed them within a single parent container object?

Lastly my API call is somewhat asymmetric. The data being sent to the API does not model 1:1 the data being stored in my database further lowering the value of any returned data from my POST.

Best Answer

I think about POSTing an array or other collection of values which should be more efficient but I am not sure that is very "RESTful". What do I return? A single status code across all entities? Is there a different HTTP Verb that I should consider?

REST doesn't prevent posting array of objects. You can read more about REST here. Whether to return a single status code across all entities depends on whether these are a part of a transaction. I am assuming it is not, so it will be better to return a status code for each entity indicating success or failure. POST/PUT/DELETE corresponds to create/update/delete operations. But in practice if sending a single large collection to be updated in the database(irrespective if it involves create/update/delete) I would use POST/PUT.

The client will never use the new IDs on the POSTed entities or requery them so that isn't an issue.

Since you are not using any data from the creation of new entries in the db, you don't need to return them at all. You can simply return an array of messages indicating which updates were successful.

Better to POST arrays or embed them within a single parent container object?

You don't need to create a parent object. Since an array of objects is valid JSON, you can send it to server without modification.

Related Topic