Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Header on Werkzeug-Exception

I'm currently working an a Javascript-App that has to do Cross-Origin Requests to a webservice using Werkzeug (I have access to both the javascript-client and the werkzeug-server)

Now, after some reading/searching, for my responses on the server I have added (Example-code):

response = generate_response(request)
response.headers.add('Access-Control-Allow-Origin', 'http://localhost')

My JS-App is running from http://localhost/MyApp. This works (-> processing on serverside goes through, client receives correct data), but only if generate_response returns an instance of a class inheriting Werkzeug's BaseResponse-Class.

But, and here is my question, if generate_response is returning one of Werkzeugs HttpExceptions, response doesn't have a headers field to append to. As it seems, headers are fixed in Werkzeug-Exceptions:

class HttpException(Exception):
    ....
    def get_headers(self, environ):
        """Get a list of headers."""
        return [('Content-Type', 'text/html')]
    ...

Is there an easy way to fix this, as in work around that so i can append my custom header to the exception or do i have to subclass the Exception-Baseclass to add support for my headers? Is this intentional or maybe a design-issue worth reporting? And last but not least: Am i misunderstanding something here and thats not at all the way it is supposed to be done?

like image 555
Daishy Avatar asked Nov 11 '22 20:11

Daishy


1 Answers

As of Werkzeug 1.0.0 milestone this issue has been fixed

https://github.com/pallets/werkzeug/issues/131

like image 67
est Avatar answered Nov 14 '22 23:11

est