No supported media type in accept header, expected one of ["json", "xml"]

Hi all

I want to allow xml request via xhr and I’ve got following error:

[debug] ** (Phoenix.NotAcceptableError) no supported media type in accept header, expected one of ["json", "xml"]
    (phoenix) lib/phoenix/controller.ex:1153: Phoenix.Controller.refuse/2
    (wp_admin) lib/wp_admin/web/router.ex:26: WpAdmin.Web.Router.api/2
    (wp_admin) lib/wp_admin/web/router.ex:15: anonymous fn/1 in WpAdmin.Web.Router.__match_route__/4
    (phoenix) lib/phoenix/router.ex:273: Phoenix.Router.__call__/1
    (wp_admin) lib/wp_admin/web/endpoint.ex:1: WpAdmin.Web.Endpoint.plug_builder_call/2
    (wp_admin) lib/plug/debugger.ex:123: WpAdmin.Web.Endpoint."call (overridable 3)"/2
    (wp_admin) lib/wp_admin/web/endpoint.ex:1: WpAdmin.Web.Endpoint.call/2
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4

and the request header looks like:

[{"host", "localhost:4000"}, {"connection", "keep-alive"},
 {"pragma", "no-cache"}, {"cache-control", "no-cache"},
 {"accept", "application/xml"}, {"sap-contextid-accept", "header"},
 {"accept-language", "en-US"}, {"maxdataserviceversion", "3.0"},
 {"user-agent",
  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"},
 {"referer", "http://localhost:4000/ui5"},
 {"accept-encoding", "gzip, deflate, sdch, br"}]

as you can see, the value of accept is:

{"accept", "application/xml"}

in the config file I tried as follow:

config :mime, :types, %{
  "application/xml" => ["xml"]
}

but it does not work at all.
What am I doing wrong?

Thanks

In your router you should have a call to the accepts plug, you need to add "xml" to the list of accepted media formats, like this: plug :accepts, ["xml"].

Yes I did it:

pipeline :api do
    plug :accepts, ["json", "xml"]
  end

  scope "/odata", WpAdmin.Web do

   pipe_through :api

   get "/:uri", OdataController, :get

  end

And it does not work.

Have you tried putting:

pipeline :api do
  plug :accepts ["json", "application/xml"]
end

Looking in https://github.com/elixir-lang/mime/blob/master/lib/mime.types doesn’t have xml as an extension for anything, so I think you need to specify the full mime type

I’ve got the same error like above.

Your request doesn’t list a content type header

It has to contain content type?

If the client doesn’t tell the server what the content type of the request is, how is the server supposed to know?

It is a GET request, why do I need to tell about the content type?

Apologies, I appear to have mis-read the original question!

No problem :grinning:

Did you try “mix deps.clean mime” after changing the Mime configuration? If it still does not work, can you provide a simple app that reproduces the error?

You can create a simple phoenix app and the router looks like:

  pipeline :api do
    plug :accepts, ["json", "application/xml"]
  end

  scope "/odata", WpAdmin.Web do

   pipe_through :api

   get "/:uri", OdataController, :get

  end

I make the request with pacman as follow:

and I’ve got following error:

[info] GET /odata/$metadata
[info] Sent 406 in 811µs
[debug] ** (Phoenix.NotAcceptableError) no supported media type in accept header, expected one of ["json", "application/xml"]
    (phoenix) lib/phoenix/controller.ex:1153: Phoenix.Controller.refuse/2
    (wp_admin) lib/wp_admin/web/router.ex:12: WpAdmin.Web.Router.api/2
    (wp_admin) lib/wp_admin/web/router.ex:1: anonymous fn/1 in WpAdmin.Web.Router.__match_route__/4
    (phoenix) lib/phoenix/router.ex:273: Phoenix.Router.__call__/1
    (wp_admin) lib/wp_admin/web/endpoint.ex:1: WpAdmin.Web.Endpoint.plug_builder_call/2
    (wp_admin) lib/plug/debugger.ex:123: WpAdmin.Web.Endpoint."call (overridable 3)"/2
    (wp_admin) lib/wp_admin/web/endpoint.ex:1: WpAdmin.Web.Endpoint.call/2
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4

Thanks a lot

This worked for me:

plug :accepts, ["json", "xml"]

and then in my config/config.exs:

config :mime, :types, %{
  "application/xml" => ["xml"]
}

and then in the terminal: mix deps.clean mime

after mix deps.clean mime to I need to execute mix deps.get ?

Phoenix master also has a new and beautiful error message:

** (Phoenix.NotAcceptableError) no supported media type in accept header.

Expected one of ["json"] but got the following formats:

  * "text/foobar" with extensions: []

To accept custom formats, register new extensions in the `:mime` library:

    config :mime, :types, %{
      "application/xml" => ["xml"]
    }

And then run `mix deps.clean --build mime` to force it to be recompiled.

Yes! You can pass the --build flag to clean the next time to not need to fetch it again.

What does it mean:

warning: the dependency mime is not present in the build directory

It was already deleted.

It works perfectly.
Thanks so much jose and for creating awesome language.