Elixir Phoenix custom JSON send response

First of all, thank you all for joining this saga for emulating file hosting in Phoenix.

For those who’re new, I’m trying to add a simple file browsing and download service (limited to json files); I can’t use database function for that thanks to “hard requirements”; I can’t use ecto for that.

I created a small custom struct for users to view invoices by struct than a full ecto schema; I can’t use user_json.ex to render my JSON output.

Anyway, here’s my controller to view one JSON file

def show_invoice(conn, %{“invoice_name” => invoice_name}) do

{:ok, invoice_json} = File.read("invoices/#{invoice_name}")
send_resp(conn, :ok, "#{invoice_json}")

end

I checked that invoice_json gives result in IO.inspect.

send_resp sends back 200 ok in my client but the body #{invoice_json} doesn’t render.

How to solve?

If you’re not working with the data consider using Phoenix.Controller — Phoenix v1.8.0-rc.0

conn
|> put_resp_header("content-type", "application/json")
|> send_resp(:ok, invoice_json)

Found this quick and dirty solution that is good enough for my needs for now until I can modify Ecto for structs in the long run