How to gzip responses from 1 endpoint in Phoenix?

Background

We have an old app using Phoenix 1.2 and we have dozens of endpoints. We want 1 of these endpoints to have its responses compressed via gzip (or whatever the default is that Phoenix uses).

Code

We have all our endpoints defined in a file like this:

defmodule MyApp.Router do
 use MyApp.Web, :router  scope "/", MyApp do
   get("/bananas", BananasController, :bananas)
 end
end

However, after checking specifications for Phoenix 1.2, we didn’t find a way to compress only 1 endpoint.

We found something in SO, but we don’t understand how we can apply this to our app:

Question

How do we adapt our code to compress responses from only the /bananas endpoint?

There is no way to do such, as this is feature of the adapter (Cowboy in most common example), not endpoint per se. So unless you run separate adapter for that controller then there is no way to compress responses.

1 Like

maybe use send_resp and do it manually eg use :zlib.gzip(response) and add the appropriate headers?

2 Likes

That’s the only way we can do it, apparently, yes.

1 Like