On Thu, Dec 15, 2016 at 12:06 PM, 'dcs3spp' via pylons-discuss <
[email protected]> wrote:

> 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/101
>
> Have 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.
>

The exception responses will auto-select the appropriate response by
default, however this is only the case when you don't override the body
[1]. In your case your custom exceptions are fully specifying that they are
json. However like I said the default exceptions raised by pyramid for
notfound/forbidden are caught and have their own default body, and based on
[1] will return json or html or plain text. If you want this to always be
json, just like your example Response(content_type=...,
body=json.dumps(...)), then you should override the notfound/forbidden
views in your app. If you don't believe what I'm saying about this (or not
seeing it in your own app) then you will need to submit a reproducible
example to talk about it further.

For example, the most basic pyramid app that always shows a notfound view:

from pyramid.config import Configurator
from wsgiref.simple_server import make_server

config = Configurator()
app = config.make_wsgi_app()
server = make_server('127.0.0.1', 8080, app)
server.serve_forever()


curl -D - -H 'Accept: application/json' http://localhost:8080

HTTP/1.0 404 Not Found
Date: Thu, 15 Dec 2016 19:35:04 GMT
Server: WSGIServer/0.2 CPython/3.5.2
Content-Type: application/json
Content-Length: 105

{"message": "The resource could not be found.\n\n\n/\n\n", "code": "404 Not
Found", "title": "Not Found"}

curl -D - -H 'Accept: text/html' http://localhost:8080

HTTP/1.0 404 Not Found
Date: Thu, 15 Dec 2016 19:36:32 GMT
Server: WSGIServer/0.2 CPython/3.5.2
Content-Type: text/html; charset=UTF-8
Content-Length: 153

<html>
 <head>
  <title>404 Not Found</title>
 </head>
 <body>
  <h1>404 Not Found</h1>
  The resource could not be found.<br/><br/>
/


 </body>
</html>

[1]
https://github.com/Pylons/pyramid/blob/1.7-branch/pyramid/httpexceptions.py#L245

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAKdhhwHdEadLmrb9iY%3DnDqC2c%2BOvxzvPVMSGTVBtWpHSXp0cYw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to