NaN

NaN

Respond with json string without render from :view or :controller

Im receiving json from postgres. I dont want to deserialize as a map then serialize to json.

Im using using rustler to query the database in rust. Which returns a string. I would like to then just pass that string back to phx then on to the user.

Any ideas how I might do this?

Marked As Solved

mindok

mindok

In your controller function you can set headers and write responses directly to the connection.

This example is for a CSV, but JSON should be pretty much identical (you may need to inspect some web response headers from something that works to get the exact recipe right)

defmodule MyWeb.ExportCSVController do
  use MyWeb, :controller

  def export(conn, %{"id" => id}) do
    csv_data = generate_csv(id)

    conn
    |> put_resp_content_type("text/csv")
    |> put_resp_header(
      "content-disposition", 
      ~s[attachment; filename="export_file.csv"]
    )
    |> put_root_layout(false)
    |> send_resp(200, csv_data) # This is where you dump the content
  end
  
  defp generate_csv(id), do: ...
end

and in your router you will have something like:

get "/export/:id", MyWeb.ExportCSVController, :export

Where Next?

Popular in Questions Top

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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement