shaare.it

KeyError: 'REQUEST_METHOD' in WSGI environ

7 Dec 2025

1 min read

KeyError: REQUEST_METHOD missing

$ flask run
Traceback (most recent call last):
  ...
KeyError: 'REQUEST_METHOD'

Why this happens

Custom middleware or test setups that create a WSGI environ without required keys can cause this error. Flask expects REQUEST_METHOD, PATH_INFO, and other WSGI variables. Manually constructing requests or misusing the test client may omit them.

Fix

  • Use Flask’s test client (app.test_client()) or app.test_request_context() to generate valid requests.
  • Ensure middleware preserves core WSGI keys and delegates correctly.

Wrong code

from flask import Flask, Request
app = Flask(__name__)

# building environ by hand incorrectly
env = {}
req = Request(env)  # raises

Fixed code

from flask import Flask
app = Flask(__name__)

with app.test_request_context('/'):
    from flask import request
    print(request.method)