sezaru
Parse json part of a multipart request
I have a route where I need to receive an uploaded image and a json payload, to do that, I’m using multipart/form-data.
The issue is that when I receive the payload in my controller, the json part is not decoded properly, I receive it in string form.
Here is my curl request, as you can see, I am passing the corret content-type for the json payload:
curl -X POST http://localhost:4000/api/qr_code/simple \
-H "Content-Type:multipart/form-data" \
-F "file=@logo.svg;type=image/svg+xml" \
-F "payload={\"my\":\"json\"};type=application/json"
The only way I managed to achieve that was to create a plug myself that do the decode:
defmodule MyParser do
@moduledoc false
@behaviour Plug
def init(opts) do
key = opts |> Keyword.fetch!(:key) |> to_string()
key
end
def call(%{req_headers: req_headers} = conn, key) do
with {"content-type", content_type} <- List.keyfind(req_headers, "content-type", 0),
true <- String.starts_with?(content_type, "multipart/form-data") do
maybe_parse_key_content(conn, key)
else
_ -> conn
end
end
defp maybe_parse_key_content(%{body_params: body_params} = conn, key) do
with {:ok, content} when is_binary(content) <- Map.fetch(body_params, key),
{:ok, decoded_content} <- Jason.decode(content) do
put_in(conn, [Access.key(:body_params), key], decoded_content)
else
_ -> conn
end
end
end
It works, but I feel that Phoenix should do that automatically.
So, is there some way to tell the Phoenix Controller to properly parse that field for me?
First Post!
codeanpeace
Most Liked
LostKobrakai
You’re assuming that’s the correct way to handle it because that’s your usecase here. People do upload json files on file inputs and they’re expected to not be treated differently than other files uploaded through the same input field. If you need certain files on the request to be treated differently that’s for you to implement.
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









