1Adding HTTP Method Overrides 2============================ 3 4Some HTTP proxies do not support arbitrary HTTP methods or newer HTTP 5methods (such as PATCH). In that case it's possible to "proxy" HTTP 6methods through another HTTP method in total violation of the protocol. 7 8The way this works is by letting the client do an HTTP POST request and 9set the ``X-HTTP-Method-Override`` header. Then the method is replaced 10with the header value before being passed to Flask. 11 12This can be accomplished with an HTTP middleware:: 13 14 class HTTPMethodOverrideMiddleware(object): 15 allowed_methods = frozenset([ 16 'GET', 17 'HEAD', 18 'POST', 19 'DELETE', 20 'PUT', 21 'PATCH', 22 'OPTIONS' 23 ]) 24 bodyless_methods = frozenset(['GET', 'HEAD', 'OPTIONS', 'DELETE']) 25 26 def __init__(self, app): 27 self.app = app 28 29 def __call__(self, environ, start_response): 30 method = environ.get('HTTP_X_HTTP_METHOD_OVERRIDE', '').upper() 31 if method in self.allowed_methods: 32 environ['REQUEST_METHOD'] = method 33 if method in self.bodyless_methods: 34 environ['CONTENT_LENGTH'] = '0' 35 return self.app(environ, start_response) 36 37To use this with Flask, wrap the app object with the middleware:: 38 39 from flask import Flask 40 41 app = Flask(__name__) 42 app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app) 43