How can I override HTTP methods for PUT and DELETE in a jar module?

I have a hard time changing the flask request object before routing happens.

My API module (and not my Flask application) depends on falsifying PUT and DELETE operations by sending a special header. I need to check the contents of the -Method header and change the request object to the jar accordingly before Flask performs its routing.

This is a short, pythonic, explicit code that I would like to use:

@api.before_request def method_scrubbing(): if request.headers.has_key('-Method'): method = request.headers['-Method'].upper() tagalog.log("in before_request, -Method = {}".format(method), 'force') if method not in ['PUT', 'DELETE']: raise ApiMethodException(method) else: request.method = method 

... but I get a read-only error from werkzeug: http://drktd.com/74yk

It seems to me that the Armin post is at http://flask.pocoo.org/snippets/38/ , but this is similar to the application (does not apply to the module).

+7
source share
1 answer

Werkzeug has speculated that the request is modified only in the WSGI middleware or before Werkzeug accesses the data. The reason is that in this way Werkzeug does not have to monitor the WSGI environment to see if caching or behavior change is canceled.

In this particular case, you can be successful, however, if you are careful by changing the underlying WSGI environment:

 request.environ['REQUEST_METHOD'] = 'something' 

After that, request.method should show β€œsomething,” and the behavior should change to form the parsing. I have not tried this and do not know if this will work. Personally, I would write middleware that performs rewriting for the entire application, or perhaps uses a simple sample url prefix for this behavior.

+6
source

All Articles