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

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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