To prevent unexpected behaviors due to content type misinterpretations
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
match _ do
send_resp(conn, 200, "Hello, world!")
end
end
The Elixir code does not set the Accept header or validate the Content-Type of the incoming requests. This could lead to unexpected behaviors when the application interprets incorrect content types.
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
match _ do
case get_req_header(conn, "content-type") do
["application/json"] -> send_resp(conn, 200, "Hello, world!")
_ -> send_resp(conn, 406, "Not Acceptable")
end
end
end
The secure Elixir code checks the Content-Type of the incoming requests and only allows application/json. If the Content-Type is different, the application responds with a 406 Not Acceptable status code.