sezaru

sezaru

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?

Showing Posts 1 to 5

codeanpeace

codeanpeace

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

sezaru

sezaru OP

I did, but I couldn’t make it work for the above case

LostKobrakai

LostKobrakai

You want to look at Plug.Parsers.MULTIPART — Plug v1.20.2 to customize how you treat the json file vs the image file. On a multipart request all you get are named files with data. The parser cannot tell that you want json to be further parsed, while the image is meant to be kept as an “uploaded file”.

sezaru

sezaru OP

Thanks, I will take a look at that plug.

But just a question about this part:

The parser cannot tell that you want json to be further parsed

Why is that? I do see that the information that the json type is application/json is sent to the server, shouldn’t the plug parser see that and automatically parse it anyway?

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews