On 11/14/2017 09:25 AM, [email protected]
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          [email protected]
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 [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/ouhl59%242cf%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to