Reputation: 525
I'm currently implementing a REST API that for some subset of resources needs to support both singular and bulk creation of said resources. I am trying to decide whether it is necessary to expose two separate endpoints: one for singular creation and one for bulk creation.
For example, let's assume I have an address resource. I can create a single address by POSTing a representation of that resource to the URI /address
. If I wanted to support bulk creation, would it better here to have the /address
endpoint discern whether multiple representations are being POSTed to that endpoint and then take the appropriate action (either a singular creation or a bulk creation), or would it better to have a totally separate endpoint which only accepts bulk creation?
Upvotes: 3
Views: 346
Reputation: 13682
My approach would be to allow a POST to /addresses with an object that can contain multiple address definitions in it. On the back end you iterate through the object and create the one or more addresses. So the body would be something like:
<addresses>
<address>
<street>123 Maple Lane</street>
<city>Newark</city>
<state>NJ</state>
<zip>00000</zip>
</address>
<address>
<street>227 First Avenue</street>
<city>Tulsa</city>
<state>OK</state>
<zip>00000</zip>
</address>
</addresses>
I would avoid a separate endpoint, as there's no reason for it. If you only post one address in the collection, only one gets created.
Upvotes: 1