Multipart file upload results in 415 Unsupported Media Type

phoenix
file-uploading

#1

I’m making a multipart upload. Its request headers look like:

Accept:application/json
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Content-Length:733
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary9Am5omyM263xjTB2
Host:localhost:4200
Origin:http://localhost:4200
Referer:http://localhost:4200/users/asdf/profile
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1

I’m getting 415 Unsupported Media Type as a response. However, I believe Phoenix is configured to accept multipart requests. In my config:

config :mime, :types, %{
  "application/json" => ["json"],
  "application/vnd.api+json" => ["json-api"],
  "multipart/form-data" => ["multipart"]
}

And in my router:

plug :accepts, ["json", "json-api", "multipart"]

After changing the config, I ran mix deps.clean --build mime.

Any idea what’s going on? I wonder if it has to do with the Accept-Enconding? Or perhaps the Content-Type boundary?


#2

I’m digging into this some more, but as far as I can tell, everything is as it should be.

The error appears to be coming from Plug, which would imply that the problem isn’t with my config/router, but rather with endpoint.ex. However, the parser setup is totally standard (and even explicitly supports :multipart):

plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  json_decoder: Poison

Anyone with more Phoenix experience have a sense for what’s going on? I’m new to Phoenix and Elixir, but looking through the code, this error really shouldn’t be triggering since pass: ["*/*"]. Unless I’m missing something?


#3

Turns out the problem was that I was piping the request through ja_serializer. In particular, due to this line. Obviously, this wasn’t a json:api request, so it shouldn’t have been piped through there anyway.