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

myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement