csalex

csalex

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?

Marked As Solved

krasenyp

krasenyp

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.

Also Liked

csalex

csalex

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

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement