Create custom middleware with FastAPI
microapis
View ChannelAbout
Hi! I'm Jose Haro Peralta, or just Jose. I'm a consultant, author, and insturctor based in London. I'm the author of Microservice APIs (https://www.manning.com/books/microservice-apis), the founder of https://microapis.io, and the creator of some popular open source libraries like fencer (https://github.com/abunuwas/fencer). Check out my latest online course: 𝐁𝐮𝐢𝐥𝐝 𝐀𝐏𝐈𝐬 𝐰𝐢𝐭𝐡 𝐏𝐲𝐭𝐡𝐨𝐧: 𝐅𝐚𝐬𝐭𝐀𝐏𝐈 𝐄𝐝𝐢𝐭𝐢𝐨𝐧 👇 ✅ https://microapis.teachable.com/p/build-apis-with-fastapi For years I've worked with companies big and small all around the world to help them build microservices, deliver API integrations, automate their infrastructure, write better code, and adopt best practices. My goal in this channel is to share with you everything I've learned over the past years about writing software and architecting systems. I also write regularly in my newsletter 👇 ✅ https://microapis.substack.com/ If you find my content interesting, don't forget to subscribe to my channel!
Latest Posts
Video Description
Create custom middleware with FastAPI The code for this tutorial is available on GitHub: https://github.com/abunuwas/short-tutorials/tree/main/fastapi-middleware What is middleware? Middleware is a layer that pre-processes requests in web servers. Before a request reaches our controllers, i.e. the functions that implement endpoints/URLs in our API, there's a pre-processing stage. In that stage, the framework parses the request, validates the headers, the content type, the data types, and so on. Most web development frameworks allow us to create custom middleware, and this is incredibly useful to add custom pre-processing components. For example, we may want to check for custom headers in the request. We can also use middleware to handle authorization and authentication, to log requests, to implement rate-limiting/throttling policies, to track user interactions, and more. We have two ways of creating custom middleware in FastAPI: - Decorator-based middleware - Class-based middleware Decorator-based middleware has a simple and nice interface and it's very convenient for small and simple middleware components. Class-based middleware is suitable for complex components that need to be isolated and encapsulated into their own modules. In this video, I show you how to create middleware in FastAPI using both approaches. We implement a very simple API, and then we add a simple middleware that checks whether requests have a custom header X-User-Type and its value. It's best practice to prefix customer headers with X- to easily tell them apart from standard headers. In future videos, I'll show you how to create middleware for Flask and Django, and how to create more complex middleware. Let me know what you liked and didn't like about this video, and what other topics you'd like me to cover! If you want to learn to build production-grade APIs with FastAPI and SQLAlchemy, sign up for "Build APIs with Python". Use the coupon 30-OFF-340EF7ED to obtain a 30% discount: https://sso.teachable.com/secure/1592930/checkout/4737131/build-apis-with-fastapi 00:00 Introduction 02:30 Setting the environment (Poetry) 04:15 Creating server.py 04:48 Configuring the environment in the DE (Pycharm) 05:35 Creating a hello world endpoint 06:12 Running the API server 07:10 Project requirements 08:32 Types of middleware in FastAPI 08:52 Creating a decorator-based middleware 12:22 Testing the middleware 14:07 Implementing class-based middleware 17:50 Testing the class-based middleware 19:02: Wrapping up If you liked this video, please like it and share it with your network, and subscribe to my channel! I'm also the author of the following Manning books: - "Microservice APIs" (http://mng.bz/jy4x). Get a 40% discount using the following code: slperalta (reach out to me if you have issues using the code). - "Secure APIs" (https://mng.bz/4JVg). This book is currently available in early access, which means only the first few chapters are published and you'll get access to more as I progress in my writing. You also get a chance to give me feedback and help me improve the book. Get a 40% discount using the following code: watchperalta40 (reach out to me if you have issues using the code). You can also download two chapters of the book for free from this link: https://microapis.io/resources/microservice-apis-in-python Check out my newsletter for advanced content on APIs and distributed systems: https://microapis.substack.com/ Let me know also what other kinds of topics you'd like me to address in future videos!
Create Your Own Slime Magic
AI-recommended products based on this video

Hydrocolloid Bandages for Wound Care, Extra Large Medical-Grade Hydrocolloid Roll with Self-Adhesive, Ultra Absorbent, Flexible & Waterproof, DIY Size & Shape First Aid Kit Essential (2in*4.8ft)

Zadin 2PCS Bald Cap for Men Women Costume, Wig Head Cap, Mr Clean Costume, Halloween Latex Dr Nefario Bald Kit

Rain-X 600001 Windshield Repair Kit - Quick and Easy Durable Resin Based Windshield Repair Kit for Chips and Cracks, Good for Round Damage Below 1" in Diameter and Cracks Smaller Than 12" in Length

GOOACC - GRC-33 240PCS Bumper Retainer Clips Car Plastic Rivets Fasteners Push Retainer Kit Most Popular Sizes Auto Push Pin Rivets Set -Door Trim Panel Fender Clips for GM Ford Toyota Honda Chrysler

