from pyramid.httpexceptions import HTTPNotFound
def error_handler(self, message):
response = HTTPNotFound()
response.body = dumps({'message': message})
response.content_type = 'application/json'
raise response
On Nov 15, 2017, at 08:05, Tres Seaver <tse...@palladion.com> wrote:On 11/14/2017 09:25 AM, webm...@johlia.de
wrote:I have a pyramid application. How can I change the content_type to
problem+json (as defined in: https://tools.ietf.org/html/rfc7807#page-9)
Interesting -- thanks for pointing that out! I'd never come across that
RFC before.My code looks like this:
from pyramid.httpexceptions import HTTPNotFound
def error_handler(self, message):
response = HTTPNotFound()
response.body = dumps({'message': message})
response.content_type = 'application/json'
raise response
Changing the content_type to application/problem+json doesn't work.
'HTTPExecption.prepare'[1] is overriding that content type based on the
'Accept:' header detection. Making that method aware of possible extension
types seems like a reasonable choice: the fastest way to get that in place
would be a pull request with tests.
In the meanwhile, ISTM that you'll either need to monkey-patch
'HTTPResponse.prepare' or else stop using exception types which derive from
'HTTPException'. E.g.:
from pyramid.response import Response
class ProblemResponse(Response, Exception):
pass
def error_handler(self, message):
raise ProblemResponse(
body=dumps({'message': message},
status='404 Not Found',
content_type = 'application/problem+json',
)
[1]https://github.com/Pylons/pyramid/blob/cc6a6ed60260b0391aa38f372b26311d58edf0d6/pyramid/httpexceptions.py#L248-L316
Tres.
--
===================================================================
Tres Seaver +1 540-429-0999 tse...@palladion.com
Palladion Software "Excellence by Design" http://palladion.com
--
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.
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/ouhl59%242cf%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.
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-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/ouhl59%242cf%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/9D026DB3-6B57-4EC3-B09E-8A19FA7E7E10%400x58.com.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-discuss@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@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/ouhl59%242cf%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.
--
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.
To post to this group, send email to pylons-...@googlegroups.com.
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-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/d755b129-1c4d-4f55-aba4-a08b6bf5d54f%40googlegroups.com.