--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/b3d1eb75-5e46-409d-95a5-dc10061b9009%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
The default exceptions raised when the framework determines notfound and forbidden errors are reliant on the request's Accept headers to determine which response to see. If you're seeing html it's because the request favored it over application/json. You can override these exception views and then return whatever response you'd like via the notfound_view_config and forbidden_view_config decorators [1].As an aside, you could be doing `json_body=myErrorDict` instead of doing the json.dumps yourself if you wanted.
On Thu, Dec 15, 2016 at 9:47 AM, 'dcs3spp' via pylons-discuss <pylons-...@googlegroups.com> wrote:
Hi,Wonder if anyone can help/advise with this query.I am using the built in HTTPException hierarchy to raise a JSON exception with the use of content_type and body parameters, e.g.raise HTTPBadRequest (content_type='application/json', body=json.dumps(myErrorDict)).This works fine rendering JSON for exception types other than HTTPNotFound and HTTPForbidden.The documentation at http://docs.pylonsproject.org/projects/pyramid/en/latest/api/httpexceptions.html#module-pyramid.httpexceptionsstates that when these types of exception are RAISED the appropriate default view (not found or forbidden) will be called. This is indeed the case when testing and always appear to be rendered as HTML, despite setting the content_type and body parameters as JSON.I have noticed that if I RETURN HttpNotFound exception as a response then JSON is rendered, e.g.return HTTPNotFound (content_type='application/json', body=json.dumps(myErrorDict))In my application I raise a HTTPNotFound exception whenever a resource id for a GET request cannot be found in the backend database. Currently this directs me to the HTML landing view for not found errors.In this case is it acceptable to RETURN HTTPNotFound so that I can achieve a JSON response? Or, should I be using a different exception type when a resource cannot be located in the database? This way I am not confusing HTTPNotFound exception with the scenario where the server did not find anything matching the Request-URI? What are other developers opinions on this?CheersSimon
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "pylons-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pylons-discuss/Rm9S34IM9fY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuoJk-N%3DbA%2B2A-HTCMEu8JhyMSw%2BOgv56bcCgUV6%3DK%2B5Nw%40mail.gmail.com.
Cheers Michael. Thanks for responding :)Can't seem to locate where request is favouring HTML over application/json. I have tested using curl request to set the accept and content-type headers as follows:curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://localhosthost:6543/assignments/101Have also tried passing in headers dictionary with Accept=application/json and Content-Type=application/json values to the HTTPNotFound exception class but still renders HTML. The only way I can seem to get JSON response is if I RETURN a HTTPNotFound exception or use the notfound and forbidden_view decorators as suggested. Will probably go down the route of using the decorators to render JSON for unmatched URLs AND resource not found urls.