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
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
preflight_explicit_allow_origin
instance-attribute
¶
preflight_explicit_allow_origin = (
preflight_explicit_allow_origin
)
is_allowed_origin
¶
is_allowed_origin(origin)
PARAMETER | DESCRIPTION |
---|---|
origin |
TYPE:
|
Source code in starlette/middleware/cors.py
93 94 95 96 97 98 99 100 101 102 |
|
preflight_response
¶
preflight_response(request_headers)
PARAMETER | DESCRIPTION |
---|---|
request_headers |
TYPE:
|
Source code in starlette/middleware/cors.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
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
142 143 144 145 146 |
|
send
async
¶
send(message, send, request_headers)
PARAMETER | DESCRIPTION |
---|---|
message |
TYPE:
|
send |
TYPE:
|
request_headers |
TYPE:
|
Source code in starlette/middleware/cors.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
allow_explicit_origin
staticmethod
¶
allow_explicit_origin(headers, origin)
PARAMETER | DESCRIPTION |
---|---|
headers |
TYPE:
|
origin |
TYPE:
|
Source code in starlette/middleware/cors.py
173 174 175 176 |
|
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
10 11 12 13 14 15 |
|
It can be imported from fastapi
:
from fastapi.middleware.gzip import GZipMiddleware
fastapi.middleware.httpsredirect.HTTPSRedirectMiddleware
¶
HTTPSRedirectMiddleware(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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
It can be imported from fastapi
:
from fastapi.middleware.trustedhost import TrustedHostMiddleware
fastapi.middleware.wsgi.WSGIMiddleware
¶
WSGIMiddleware(app)
It can be imported from fastapi
:
from fastapi.middleware.wsgi import WSGIMiddleware