sezaru

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

codeanpeace

Have you taken a look at the Plug documentation and the built-in parsers?

Most Liked

LostKobrakai

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.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement