Registry / web-framework / flask-request-id-header

flask-request-id-header

JSON →
library0.1.1pypypi✓ verified 92d ago

Flask-Request-ID-Header is a Python Flask middleware designed to ensure that all incoming requests to a Flask application include an `X-Request-ID` header, containing at least one unique value. This is particularly useful for request tracing and correlation in distributed systems and logging. The current version is 0.1.1, and the project has not seen active development since its last release in March 2019, indicating a maintenance-only status.

pip install flask-request-id-header
INSTALL
IMPORT
SIG · FLASK-REQUEST-ID-H
F
flask-request-id-header
web-frameworkpythonv0.1.1
Install
2.2s avg
Import
471ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.489s · 22.5MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 2.2s · import 0.453s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RequestID
✓ from flask_request_id_header.middleware import RequestID

Initializes a Flask app and applies the RequestID middleware. It then defines a simple route to demonstrate how to retrieve the `X-Request-ID` header from the request.

from flask import Flask, request from flask_request_id_header.middleware import RequestID app = Flask(__name__) RequestID(app) @app.route('/') def hello_world(): request_id = request.headers.get('X-Request-ID', 'No-Request-ID-Found') return f'Hello, world! Request ID: {request_id}' if __name__ == '__main__': app.run(debug=True)
Debug
Known issues
deprecatedThe 'X-' prefix for custom HTTP headers, such as 'X-Request-ID', is technically deprecated by W3C standards, which recommend formal header naming conventions. While still widely supported and a de-facto standard, this might be a consideration for future-proofing.
fix
No direct fix needed for current functionality, but be aware of evolving HTTP header conventions.
affects: All versions
gotchaThe middleware automatically appends a new UUID (version 4) to the 'X-Request-ID' header if an existing value does not already contain at least one UUID v4. If you wish to preserve a custom non-UUID request ID provided by a client, you must configure `app.config['REQUEST_ID_UNIQUE_VALUE_PREFIX']` with a prefix that your custom IDs will use.
fix
Set `app.config['REQUEST_ID_UNIQUE_VALUE_PREFIX'] = 'YOUR_PREFIX-'` to prevent modification of headers starting with 'YOUR_PREFIX-'.
affects: All versions
gotchaThis library focuses on ensuring the 'X-Request-ID' header exists and is correctly formatted. It does not automatically integrate the request ID into Flask's default logging system. To include the request ID in your application logs, you will need to implement a custom logging filter or use a more comprehensive logging library.
fix
Manually configure a `logging.Filter` to inject `request.headers.get('X-Request-ID')` into log records.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flask_request_id_header.middleware'
The `flask-request-id-header` package is not installed in the current environment, or the import path `from flask_request_id_header.middleware import RequestID` is incorrect.
fix
Ensure the package is installed: `pip install flask-request-id-header`. Verify the import statement matches the correct path.
KeyError: 'X-Request-ID' when accessing `request.headers['X-Request-ID']`
Attempting to access the `X-Request-ID` header from the Flask `request` object outside of an active request context (e.g., during application setup) or before the middleware has processed the request. While the middleware ensures the header, access without a proper request context will fail.
fix
Access the header within a request context (e.g., inside a route function, a `before_request` handler, or using `app.test_request_context()`). Use `request.headers.get('X-Request-ID', 'default_value')` to safely retrieve the header without raising a `KeyError` if it might be missing in certain scenarios.
AttributeError: 'Request' object has no attribute 'request_id' or NameError: name 'g' is not defined when trying to access `g.request_id`
This specific library (flask-request-id-header) does not expose the request ID via `request.request_id` or `flask.g.request_id` by default. These patterns are common in *other* Flask request ID extensions.
fix
Access the request ID directly from the request headers dictionary: `request.headers.get('X-Request-ID')`.
Upgrade
Version history
0.1.1latest on PyPI · released Mar 2, 2019
Audit
Dependencies
FlaskrequiredCore web framework dependency for the middleware.
Agent activity
12 hits · last 30 days
node
10
Resources
flask-request-id-header — pip install flask-request-id-header · libregistry