0

I'm using Asp.net Core 2.0 Web API to create some database entries in SQL Server.I don't want to create duplicate rows and i want to inform the consumer of the API know that it's trying to create duplicate rows. i have two controller actions to create these entries witch the first one Accepts only one instance of the object to be created and the second one accept a List<object> all to be created. for the first action method if the row is duplicate i respond with a Http Conflict(409) error and if not i resend the processed object back to the consumer inside an OK Http response.

In the second action method i am getting a list of the objects and maybe some of them are duplicate and some are not. I'm trying to do a best practice by adding the non-duplicate entries and sending back an error for duplicate ones. the problem here is that how should be my response? an OK response or Conflict? further more i want the consumer to know witch objects are created and witch ones not, for the added ones i want to send back the object itself in a list and for the duplicate ones i want to send them back with a duplicate flag!. here it's important for met to get a best practice solution and not just a complex solution.

TL;DR There is an action of post method for a Web API to Create a List of the objects, How the Respond must be when some of the objects are duplicate and others are not in a clear and best practice way.

yekanchi
  • 813
  • 1
  • 12
  • 30

1 Answers1

1

REST best practicies are not carved in stone and you may encounter lots of opinions on how to do things. One approach is that you do not provide group operations, exactly because of problems when reporting the results.

There are other ways to accomplish group operations. E.g. Facebook uses Batch requests, while Google uses Multipart messages for batching.

In REST you work with resources, so your POST endpoint should create a resource. You could view the whole collection of items as a resource, but than it should either succeed or fail as such. I'd first check the collection for duplicates, and if found, return 400 Bad data.

Maxim Zabolotskikh
  • 3,091
  • 20
  • 21