sezaru
Customize AshJsonApi route to return raw binary
I have an API route with AshJsonApi that generates an image that needs to be sent back to the user.
I wanted to know if there is some way to customize an route so I can change the return type from “application/json” to “image/png” and send the binary directly in the body.
I know I can achieve something similar by creating a base64 of the image binary or storing it somewhere and return an url, but I would prefer to send the raw binary back if possible.
Here is my action:
action :render, :binary do
run fn _, _ ->
some_binary = <<>>
{:ok, some_binary}
end
end
Most Liked
sezaru
Update:
So, I added this to my route to change the conn:
route :post, "render", :render do
modify_conn fn conn, subject, result, request ->
conn
|> Plug.Conn.put_resp_content_type("image/png")
|> Plug.Conn.send_resp(200, <<213>>)
end
end
This will “work”, it sends the response the way I want.. But, right after the modify_conn, ash json api will call its own controller (ash_json_api/controllers/response.ex) and try to send the json value too, giving the [error] ** (Plug.Conn.AlreadySentError) the response was already sent error since I already sent the response before.
So I guess there is no scapegoat right now to achieve that?
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
- #forms
- #api
- #metaprogramming
- #security
- #hex









