I am getting the below error while executing an GET api from supavisor. I have searched regarding this and tried some ways to fix it but didn’t get any solution. Most of the docs are related to xml and I think my error is due to text.
I am new to elixir and phoenix, can anyone suggest something to fix the error?
# Phoenix.NotAcceptableError at GET /api/health
Exception:
** (Phoenix.NotAcceptableError) no supported media type in accept header.
Expected one of ["json"] but got the following formats:
* "text/plain" with extensions: ["txt", "text"]
To accept custom formats, register them under the :mime library
in your config/config.exs file:
config :mime, :types, %{
"application/xml" => ["xml"]
}
Can you show the part from your router.ex
that’s responsible for this endpoint? And its pipeline inside the same file?
I got two parts with pipeline from router.ex
One part I have modified and given the below to accept the json and xml
For example:
defmodule MyAppWeb.Router do
use Phoenix.Router
pipeline :browser do
plug :fetch_session
plug :accepts, ["html", "json", "xml"]
end
scope "/" do
pipe_through :browser
# browser related routes and resources
end
end
The second part showing like this
pipeline :browser do
plug :fetch_session
plug :accepts, ["html"]
end
pipeline :auth do
plug :ensure_authenticated
end
scope "/" do
pipe_through [:browser, :auth]
get "/posts/new", PostController, :new
post "/posts", PostController, :create
end
scope "/" do
pipe_through [:browser]
get "/posts", PostController, :index
get "/posts/:id", PostController, :show
end
The parts you posted don’t cover the route that’s failing (/api/health
) or show the configuration that’s likely causing it - I’d guess that there’s an :api
pipeline block with plug :accepts, ["json"]
in it, based solely on the error message’s expectation:
Thank you for the update.
My application is supavisor and I configured it using Setup - supavisor
I am calling an API using curl command
Could you please suggest something that can figure out the issue?
The /api/health
route is defined here, which is routed through the :api
pipeline, which has plug :accepts, ["json"]
.
You’ll need to include the appropriate header in your curl
command, something like -H "Accept: application/json"
1 Like
Thank you @al2o3cr
Now I am able to hit the API using curl command using linux terminal but from browser it’s not working.
Is there anyway to resolve the issue with browser?
How are you making the request “with browser”?
Here are some freshly-generated router pipelines:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, html: {HelloPhoenixWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
Copy from these until you have un-broken your application.