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

1 Like

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"].

1 Like

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.

1 Like

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

1 Like

I’ve got the same error like above.

1 Like

Your request doesn’t list a content type header

2 Likes

It has to contain content type?

1 Like

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

1 Like

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

1 Like

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

1 Like

No problem :grinning:

1 Like

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?

1 Like

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

1 Like

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

1 Like

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

1 Like

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.
2 Likes

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

1 Like

What does it mean:

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

1 Like

It was already deleted.

1 Like

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

1 Like