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
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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









