1

I currently have a Phoenix application that has both JSON and HTML endpoints.

I would like to return the errors that are the most appropriate for the given request, but it always defaults to the render("XXX.html, _) function even when the request's format is application/json.

Is there a way to return JSON for requests of type application/json and HTML for everything else?

2

1 Answer 1

1

You may wanna check the render_errors option (see docs) in your config.exs file and add the json format as well as html.

config :my_app, MyApp.Endpoint,
  # ...
  render_errors: [view: MyApp.ErrorView, accepts: ~w(html json)]

Also, be sure to be sending the appropriate Accept header.

Sign up to request clarification or add additional context in comments.

1 Comment

The Accept header wasn't set. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.