- Middleware
- fastapi.middleware.cors.CORSMiddleware
- app
instance-attribute
- allow_origins
instance-attribute
- allow_methods
instance-attribute
- allow_headers
instance-attribute
- allow_all_origins
instance-attribute
- allow_all_headers
instance-attribute
- preflight_explicit_allow_origin
instance-attribute
- allow_origin_regex
instance-attribute
- simple_headers
instance-attribute
- preflight_headers
instance-attribute
- is_allowed_origin
- preflight_response
- simple_response
async
- send
async
- allow_explicit_origin
staticmethod
- app
- fastapi.middleware.gzip.GZipMiddleware
- fastapi.middleware.httpsredirect.HTTPSRedirectMiddleware
- fastapi.middleware.trustedhost.TrustedHostMiddleware
- fastapi.middleware.wsgi.WSGIMiddleware
- fastapi.middleware.cors.CORSMiddleware
Middleware
There are several middlewares available provided by Starlette directly.
Read more about them in the FastAPI docs for Middleware.
fastapi.middleware.cors.CORSMiddleware
CORSMiddleware(
app,
allow_origins=(),
allow_methods=("GET"),
allow_headers=(),
allow_credentials=False,
allow_origin_regex=None,
expose_headers=(),
max_age=600,
)
PARAMETER | DESCRIPTION |
---|---|
app | TYPE: |
allow_origins | TYPE: |
allow_methods | TYPE: |
allow_headers | TYPE: |
allow_credentials | TYPE: |
allow_origin_regex | TYPE: |
expose_headers | TYPE: |
max_age | TYPE: |
Source code in starlette/middleware/cors.py
|
|
app instance-attribute
app = app
allow_origins instance-attribute
allow_origins = allow_origins
allow_methods instance-attribute
allow_methods = allow_methods
allow_headers instance-attribute
allow_headers = [lower() for h in allow_headers]
allow_all_origins instance-attribute
allow_all_origins = allow_all_origins
allow_all_headers instance-attribute
allow_all_headers = allow_all_headers
preflight_explicit_allow_origin instance-attribute
preflight_explicit_allow_origin = (
preflight_explicit_allow_origin
)
allow_origin_regex instance-attribute
allow_origin_regex = compiled_allow_origin_regex
simple_headers instance-attribute
simple_headers = simple_headers
preflight_headers instance-attribute
preflight_headers = preflight_headers
is_allowed_origin
is_allowed_origin(origin)
PARAMETER | DESCRIPTION |
---|---|
origin | TYPE: |
Source code in starlette/middleware/cors.py
|
|
preflight_response
preflight_response(request_headers)
PARAMETER | DESCRIPTION |
---|---|
request_headers | TYPE: |
Source code in starlette/middleware/cors.py
|
|
simple_response async
simple_response(scope, receive, send, request_headers)
PARAMETER | DESCRIPTION |
---|---|
scope | TYPE: |
receive | TYPE: |
send | TYPE: |
request_headers | TYPE: |
Source code in starlette/middleware/cors.py
|
|
send async
send(message, send, request_headers)
PARAMETER | DESCRIPTION |
---|---|
message | TYPE: |
send | TYPE: |
request_headers | TYPE: |
Source code in starlette/middleware/cors.py
|
|
allow_explicit_origin staticmethod
allow_explicit_origin(headers, origin)
PARAMETER | DESCRIPTION |
---|---|
headers | TYPE: |
origin | TYPE: |
Source code in starlette/middleware/cors.py
|
|
It can be imported from fastapi
:
from fastapi.middleware.cors import CORSMiddleware
fastapi.middleware.gzip.GZipMiddleware
GZipMiddleware(app, minimum_size=500, compresslevel=9)
PARAMETER | DESCRIPTION |
---|---|
app | TYPE: |
minimum_size | TYPE: |
compresslevel | TYPE: |
Source code in starlette/middleware/gzip.py
|
|
app instance-attribute
app = app
minimum_size instance-attribute
minimum_size = minimum_size
compresslevel instance-attribute
compresslevel = compresslevel
It can be imported from fastapi
:
from fastapi.middleware.gzip import GZipMiddleware
fastapi.middleware.httpsredirect.HTTPSRedirectMiddleware
HTTPSRedirectMiddleware(app)
PARAMETER | DESCRIPTION |
---|---|
app | TYPE: |
Source code in starlette/middleware/httpsredirect.py
|
|
app instance-attribute
app = app
It can be imported from fastapi
:
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
fastapi.middleware.trustedhost.TrustedHostMiddleware
TrustedHostMiddleware(
app, allowed_hosts=None, www_redirect=True
)
PARAMETER | DESCRIPTION |
---|---|
app | TYPE: |
allowed_hosts | TYPE: |
www_redirect | TYPE: |
Source code in starlette/middleware/trustedhost.py
|
|
app instance-attribute
app = app
allowed_hosts instance-attribute
allowed_hosts = list(allowed_hosts)
allow_any instance-attribute
allow_any = '*' in allowed_hosts
www_redirect instance-attribute
www_redirect = www_redirect
It can be imported from fastapi
:
from fastapi.middleware.trustedhost import TrustedHostMiddleware
fastapi.middleware.wsgi.WSGIMiddleware
WSGIMiddleware(app)
PARAMETER | DESCRIPTION |
---|---|
app | TYPE: |
Source code in starlette/middleware/wsgi.py
|
|
app instance-attribute
app = app
It can be imported from fastapi
:
from fastapi.middleware.wsgi import WSGIMiddleware