Mutating request.base_url in Flask Mutating request.base_url in Flask flask flask

Mutating request.base_url in Flask


Turns out request is a proxied object. I'm not sure of the internals but it's "reset" on each import. I solved the issue by subclassing Request

class ProxiedRequest(Request):    def __init__(self, environ, populate_request=True, shallow=False):        super(Request, self).__init__(environ, populate_request, shallow)        # Support SSL termination. Mutate the host_url within Flask to use https://        # if the SSL was terminated.        x_forwarded_proto = self.headers.get('X-Forwarded-Proto')        if  x_forwarded_proto == 'https':            self.url = self.url.replace('http://', 'https://')            self.host_url = self.host_url.replace('http://', 'https://')            self.base_url = self.base_url.replace('http://', 'https://')            self.url_root = self.url_root.replace('http://', 'https://')app = Flask(__name__);app.request_class = ProxiedRequest