Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

returning JSON 404 #191

Open
rcapile opened this issue May 31, 2017 · 2 comments
Open

returning JSON 404 #191

rcapile opened this issue May 31, 2017 · 2 comments

Comments

@rcapile
Copy link

rcapile commented May 31, 2017

Since it is an API. When an endpoint does not exists (route is not found), shouldn't it return a 404 with response in JSON instead of error/404.phtml or some other HTTP code with a JSON response?

@weierophinney
Copy link
Member

Apigility essentially maps routes to resources, which means that if routing does not match something, we don't know if that something should be an API resource, or a general web page; as such, returning JSON may be erroneous, if the page in question might map to an HTML response. We chose not to interfere with the normal MVC workflow and use the general tooling.

If you want to return a 404 with JSON, there are a couple of possibilities.

First, if the 404 might be due to an invalid identifier, you can either throw an exception with a 404 code, or return an ApiProblemResponse from your controller or REST resource.

Second, you can hook into the MvcEvent::EVENT_DISPATCH_ERROR, and check if the event error matches Application::ERROR_ROUTER_NO_MATCH; if it does, your listener could return an ApiProblemResponse with a 404 error code.

If this second functionality is something you feel would be a good general purpose addition to Apigility, we can consider adding a listener that users can optionally register with their Apigility applications for that purpose.

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools; a new issue has been opened at laminas-api-tools/api-tools#4.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants