new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

middleware

14 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

CORSMiddleware import

CORSMiddleware can be imported from fastapi.middleware.cors: `from fastapi.middleware.cors import CORSMiddleware`. It can also be imported directly from fastapi.

GZipMiddleware import

GZipMiddleware can be imported from fastapi.middleware.gzip: `from fastapi.middleware.gzip import GZipMiddleware`. It can also be imported directly from fastapi.

HTTPSRedirectMiddleware import

HTTPSRedirectMiddleware can be imported from fastapi.middleware.httpsredirect: `from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware`. It can also be imported directly from fastapi.

TrustedHostMiddleware import

TrustedHostMiddleware can be imported from fastapi.middleware.trustedhost: `from fastapi.middleware.trustedhost import TrustedHostMiddleware`. It can also be imported directly from fastapi.

Starlette middleware availability

Several middlewares are available as provided by Starlette directly and can be used in FastAPI applications.

FastAPI supports CORS

FastAPI supports CORS (Cross-Origin Resource Sharing) through Starlette.

Proxy Forwarded Headers in FastAPI

FastAPI can interpret proxy forwarded headers when configured to do so. The main forwarded headers are X-Forwarded-For (original client IP address), X-Forwarded-Proto (original protocol such as https), and X-Forwarded-Host (original host domain). The --forwarded-allow-ips CLI option must be used to enable FastAPI to trust and interpret these headers from specific IP addresses.

FastAPI --forwarded-allow-ips CLI option

The --forwarded-allow-ips option can be passed to the FastAPI CLI to specify which IP addresses are trusted to send proxy forwarded headers. The value can be set to '*' to trust all incoming IPs, which is appropriate when the server is behind a trusted proxy. Example: fastapi run --forwarded-allow-ips='*'

FastAPI --root-path CLI option

The --root-path option can be passed to the FastAPI CLI to set the root path for the application. This is used when the application is behind a proxy with a stripped path prefix. Example: fastapi run main.py --root-path /api/v1

ASGI scope root_path for accessing current root path

The current root_path used by a FastAPI application can be retrieved from the scope dictionary which is part of the ASGI specification. This allows access to the root_path value for each request.

Proxy forwarded headers enable HTTPS redirects

When proxy forwarded headers are enabled via --forwarded-allow-ips, FastAPI can perform correct redirects using the HTTPS protocol and proper host information. Without this configuration, redirects may use incorrect protocols and hostnames such as http://localhost:8000 instead of the proper https://mysuperapp.com.

Proxy with stripped path prefix behavior

When a proxy strips a path prefix before forwarding requests to the application server, the server (such as Uvicorn with FastAPI) receives requests at the original path without the prefix. The root_path parameter informs the application about the prefix that was stripped, so the application can generate correct URLs and OpenAPI schemas that include the full path including the proxy prefix.

OpenAPI servers list automatic insertion with root_path

When a FastAPI application is created with a root_path and custom servers are provided, FastAPI automatically inserts a server entry with the root_path URL at the beginning of the servers list in the OpenAPI schema. This allows the documentation UI to work correctly when accessed through the proxy with the full path prefix.

Sub-application mounting with root_path

When mounting a sub-application in FastAPI while using root_path for a proxy, FastAPI handles the root_path internally in an intelligent way so that everything works as expected without additional configuration.

Give your agent this brain