I have a request object that comes from werkzeug. I want to change a value on this request object. This is not possible because werkzeug request objects are immutable. I understand this design decision, but I need to change a value. How do I do this?
>>> request
<Request 'http://localhost:5000/new' [POST]>
>>> request.method
'POST'
>>> request.method = 'GET'
*** AttributeError: read only property
I tried doing a deepcopy
, but the resulting copy is immutable also. I guess I could just create my own mock object and fill in the values manually, but that is my last resort solution. Is there a better way?
This is what I came up with:
def make_duplicate_request(request):
"""
Since werkzeug request objects are immutable, this is needed to create an
identical request object with mutable values
"""
class Req(object):
method = 'GET'
path = ''
headers = []
args = []
r = Req()
r.path = request.path
r.headers = request.headers
r.is_xhr = request.is_xhr
r.args = request.args
return r
Maybe no the most elegant solution, but it works.