Phoenix API batch endpoint

In our Phoenix API we have implemented a batch endpoint that executes multiple requests in a single network call. It uses Task.async_stream/3 and Phoenix.ConnTest.dispatch/5 to execute each eachrequest asynchronously and return a unified response once all requests are executed.

So far it works as expected, although we use a test related functionality (dispatch/5 is defined in Phoenix.ConnTest).

I came across an issue when an endpoint expects both query and body params. dispatch/5 expects both body and query params to be passed on body_or_params (the 5th argument of the function) and automatically converts them to query or body params based on the HTTP request method. It seems like a distinction between body and query params is not possible, judging from the source code.

I tried to explicitly set those fields on the %Plug.Conn{} struct before dispatching but again, looking at the dispatch/5 source code, the connection is recycled, essentially keeping only some cookies and relevant headers, bringing me back to the use of body_or_params parameter.

The above raise my following questions:

  1. Is there a way to differentiate body and query params using the existing functionality?
  2. On a higher level, using a test related function to achieve this batching feature does not feel as the best approach, is there any alternative to that?

You might want to look into using YourRouter.call/2 instead of Phoenix.ConnTest.dispatch/5. Using the latter in a non-test setup just feels strange to me.

1 Like

Thanks a lot, this solved the issue. This is also how dispatch/5 eventually executes the request

1 Like