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

Last Post!

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

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
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
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

We're in Beta

About us Mission Statement