I have this code
type or paste defmodule MyApp.PlugServer.Plugs.CheckRequestFields do
def init(options), do: options
def call(%Plug.Conn{request_path: path} = conn, opts) do
{:ok, body, conn} = Plug.Conn.read_body(conn, opts)
IO.inspect(body)
if path in opts[:paths], do: verify_request_fields!(conn.params, opts[:fields])
conn
end
defp verify_request_fields!(params, fields) do
miss = []
fields
|> Enum.reduce(miss, fn x ->
if Map.get(params, x) == nil do
miss ++ [x]
else
miss
end
end)
IO.inspect(fields)
end
end
here
When I try to log the body, I got "{\n\t\"user\": \"Pedri\"\n}"
althought i’m sending
{
“user”: “Pedri”
}
as body
the endpoint
defmodule TestesPay.MyRouter do
use Plug.Router
alias MyApp.PlugServer.Plugs.CheckRequestFields
plug Plug.Parsers,
parsers: [:urlencoded, :json],
json_decoder: Jason
plug CheckRequestFields, fields: ["user", "age"], paths: ["/hello"]
plug :match
plug :dispatch
get "/hello" do
send_resp(conn, 200, "world")
end
match _ do
send_resp(conn, 404, "oops")
end
end
I must remove special characters by myself or i’m missing some configuration?