django-middleware-request-id
The middleware detect if a client or the front reverse proxy server provides a X-Request-ID header, and get it as the request_id. If no such header is provided, it can provide a random value.
Install
pip install django-middleware-request-id
Usage
pro/settings.py
INSTALLED_APPS = [
"django_middleware_global_request",
"django_middleware_request_id",
]
MIDDLEWARE = [
...
"django_middleware_global_request.middleware.GlobalRequestMiddleware",
"django_middleware_request_id.middlewares.DjangoMiddlewareRequestId",
...
]
app/views.py
from django.http import HttpResponse
from django_middleware_request_id import get_request_id
def get_request_id_view(request):
request_id = get_request_id()
return HttpResponse(request_id)
app/urls.py
from django.contrib import admin
from django.urls import path
froml . import views
urlpatterns = [
path('get_request_id', views.get_request_id_view),
]
Set the request id at nginx
http {
...
# -------------------------------------------------------------------------------
# Set variable $reqid.
# If you trust client or front nginx's header, you can use `map block` here.
# -------------------------------------------------------------------------------
map $http_x_request_id $reqid {
default $http_x_request_id;
"" $request_id;
}
# -------------------------------------------------------------------------------
server {
# -------------------------------------------------------------------------------
# If you don't trust client and front nginx's header, or you are sure this
# is the first front nginx, just set the variable $reqid to a new random value.
# Use `map block` above, or use `set line` below, but don't use both.
# Uncomment the `set line` below to `set new random value`.
# -------------------------------------------------------------------------------
# set $reqid $request_id;
# -------------------------------------------------------------------------------
...
location /api/ {
proxy_pass http://backend/api/;
proxy_set_header X-Request-Id $reqid;
...
}
...
}
}
Test Passed
- python27:~=django1.11.29
- python34:~=django1.11.29
- python34:~=django2.0.13
- python35:~=django1.11.29
- python35:~=django2.0.13
- python35:~=django2.1.15
- python35:~=django2.2.28
- python36:~=django2.0.13
- python36:~=django2.1.15
- python36:~=django2.2.28
- python36:~=django3.0.14
- python36:~=django3.1.14
- python36:~=django3.2.21
- python37:~=django2.0.13
- python37:~=django2.1.15
- python37:~=django2.2.28
- python37:~=django3.0.14
- python37:~=django3.1.14
- python37:~=django3.2.21
- python38:~=django2.0.13
- python38:~=django2.1.15
- python38:~=django2.2.28
- python38:~=django3.0.14
- python38:~=django3.1.14
- python38:~=django3.2.21
- python38:~=django4.0.10
- python38:~=django4.1.11
- python38:~=django4.2.5
- python39:~=django2.0.13
- python39:~=django2.1.15
- python39:~=django2.2.28
- python39:~=django3.0.14
- python39:~=django3.1.14
- python39:~=django3.2.21
- python39:~=django4.0.10
- python39:~=django4.1.11
- python39:~=django4.2.5
- python310:~=django2.1.15
- python310:~=django2.2.28
- python310:~=django3.0.14
- python310:~=django3.1.14
- python310:~=django3.2.21
- python310:~=django4.0.10
- python310:~=django4.1.11
- python310:~=django4.2.5
- python311:~=django2.2.28
- python311:~=django3.0.14
- python311:~=django3.1.14
- python311:~=django3.2.21
- python311:~=django4.0.10
- python311:~=django4.1.11
- python311:~=django4.2.5
Releases
v0.1.0
- First release.
v0.1.2
- Fix problem that import
get_request_idfrom the package root. - Fix problem that NOT using DJANGO_REQUEST_ID_HEADER setting.
- Fix problem that call get_request_id() from none request context.
v0.1.3
- Doc update.
v0.1.4
- Unit test all passed.
v0.1.5
- Add unit test for python-12 and django-5.0.6.
v0.1.6
- Doc update.
Metadata
Release files for django-middleware-request-id 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_middleware_request_id-0.1.6.tar.gz | 6.2 kB | Without provenance | Details for django_middleware_request_id-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_middleware_request_id-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for django_middleware_request_id-0.1.6-py3-none-any.whl |
Total release size: 13.5 kB
Release files / django_middleware_request_id-0.1.6.tar.gz
| Download URL | django_middleware_request_id-0.1.6.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7e4ca93b26e61842442361b91175003292474f036948a6add5b5aed31bdf40a
|
|
BLAKE2b-256 checksum How to use checksums |
f332baeda7988be0bcf9d3723babf1753fa6c8f3a3ed496ddf6e67bb1fa8002a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Release files / django_middleware_request_id-0.1.6-py3-none-any.whl
| Download URL | django_middleware_request_id-0.1.6-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54eafb847fdf660f3ddd057fa25f7080a499d800194ab12bbe73e51452f53721
|
|
BLAKE2b-256 checksum How to use checksums |
66e7f4319f23941787dd29f38881fc5de053b8ce735fa3b118446f36cdd9de63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Security for django-middleware-request-id 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.