Api-tools. REST API. Multiple Insert using POST

Hi!
I have created a REST API (Db-connected) with API-tools. It works ok but (always there is but), I want to send one big POST request with many new entities rather than many request with one new entity (because of performance). Does the basic configuration works in that way or should I create a new REST service for that purpouse?

Thank you in advance,

It depends a bit on whether or not you’ve created validation rules for the incoming POST payload.

If you haven’t, you can do those validations in your resource.

If you have, you may want to create an RPC endpoint and either setup validations on that endpoint, or in the controller it creates. From there, have the controller compose any of the various models it needs (DB tables, Doctrine repositories, etc.), and have it do the work.

Essentially, what you do in the endpoint is up to you. The sticky point will be validation, and you can either bypass it, or make it very comprehensive.

Thank you for your answer Matthew.

I am not pretty sure to understand your answer.

As I said, I created the API just “next, next, next” with Db-connection for the tables I was interested. I just unmarked (PUT, DELETE and PATCH methods for the Entity). Api-tools auto-create the validations using the database information.

If I send by POST one entity (with its fields) it works perfect I just want to know if this basic configuration (POST is marked for Collection) is able to accept a collection of entities? If not I guess I should create another service.

With JSON {“field1”:“value1”, “field2”:“value2”} inserts it in database, with [{“field1”:“value1”, “field2”:“value2”},{“field1”:“value3”, “field2”:“value4”}] reach insert in DbConnectedResource.php and finally throws an ServerInternalError.

Once again, thank you.

If you want it to handle multiple entities, you’ll need to either:

  • disable validation (if you’ve defined any) for the service, and move validation into the resource, or
  • create a custom input filter that checks to see if a single entity or multiple entities were submitted, and then vary validation accordingly.

From there, in your resource, you’ll need to determine if you have a single entity or multiple entities, and vary how you handle them.

Thank you.

I will try.