
OTHER MIDDLEWARES
Other Middlewares in FastAPI
What Are Middlewares?
Middleware is a layer that sits between the client and the FastAPI application.
When a client sends a request, the request first passes through the middleware. The middleware can perform some work before the request reaches the API. After the API creates a response, the middleware can also process the response before sending it back to the client.
Simple Flow
Client → Middleware → FastAPI API → Middleware → Client
Middleware is useful when we want to perform a common task for many or all API requests instead of writing the same code inside every endpoint.
What Can Middleware Do?
Middleware can be used for:
- Checking authentication
- Adding security headers
- Logging requests and responses
- Measuring request processing time
- Handling CORS
- Adding or modifying response headers
- Checking requests before they reach the API
- Processing responses before sending them to the client
- Handling common application-level tasks
Types of Middleware
FastAPI provides different middleware options for different requirements.
1. CORS Middleware
CORS stands for Cross-Origin Resource Sharing.
It controls whether a frontend application from one origin can access an API from another origin.
Example
Suppose:
- Frontend →
http://localhost:3000 - FastAPI →
http://localhost:8000
These are different origins.
CORS middleware allows us to specify which frontend origins are allowed to communicate with our FastAPI API.
Simple meaning:
CORS decides who is allowed to access our API from another website or application.
2. Trusted Host Middleware
Trusted Host Middleware helps protect the application from requests containing an invalid Host header.
For example, we can specify that our application should accept requests only for:
example.com
If a request comes with an unexpected host, the middleware can reject it.
Simple meaning:
It checks whether the request is coming through an allowed host/domain.
3. HTTPS Redirect Middleware
HTTPS Redirect Middleware redirects HTTP requests to HTTPS.
Example
User requests:
http://example.com
The middleware redirects the request to:
https://example.com
HTTPS is important because it encrypts communication between the client and server.
Simple meaning:
It makes sure users access the application using HTTPS instead of HTTP.
4. GZip Middleware
GZip Middleware compresses HTTP responses before sending them to the client.
For example, if the API response is large, compression can reduce the amount of data that needs to be transferred.
Flow
API Response → GZip Compression → Client
The client then decompresses the response.
Simple meaning:
It makes large responses smaller and faster to transfer.
5. Authentication Middleware
Authentication middleware can check whether a user is authenticated before allowing access to protected resources.
For example, a request may contain an authentication token.
The middleware can:
- Receive the request
- Check the token
- Decide whether the request is allowed
- Continue to the API if valid
- Reject the request if invalid
Simple meaning:
It checks who the user is before allowing access.
6. Logging Middleware
Logging middleware records information about requests and responses.
For example:
- Request method:
GET - URL:
/users - Status code:
200 - Request processing time
This helps developers understand what is happening inside the application.
Simple meaning:
It keeps a record of API activity.
7. Custom Middleware
We can also create our own middleware when the application needs a specific task.
For example, we can create middleware to calculate how much time an API request takes.
Flow
Request → Start Timer → API → Stop Timer → Response
The middleware can calculate:
Processing Time = End Time - Start Time
This is useful for monitoring API performance.
Why Use Middleware?
Without middleware, we may need to write the same common logic inside many endpoints.
For example, if we want to log every request, instead of adding logging code to 50 APIs, we can put the logging logic in one middleware.
Then:
Request 1 → Middleware → API
Request 2 → Middleware → API
Request 3 → Middleware → API
The middleware automatically handles the common task.
Join Techsnap Creators
Share your knowledge and earn ??
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments