Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

Create custom middleware with FastAPI

Автор: microapis

Загружено: 2024-06-04

Просмотров: 3598

Описание:

Create custom middleware with FastAPI

The code for this tutorial is available on GitHub: https://github.com/abunuwas/short-tut...

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/1592...


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/micros...

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 custom middleware with FastAPI

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

Похожие видео

Tak Dania niszczyła Grenlandczyków! Miśko: To co robili na Grenlandii było straszne!

Tak Dania niszczyła Grenlandczyków! Miśko: To co robili na Grenlandii było straszne!

Are you scripting against ArcGIS Online or Portal with the ArcGIS API for Python? See the difference

Are you scripting against ArcGIS Online or Portal with the ArcGIS API for Python? See the difference

Validate JWTs issued by Auth0 in FastAPI

Validate JWTs issued by Auth0 in FastAPI

Я в опасности

Я в опасности

API Security Fundamentals [2023]

API Security Fundamentals [2023]

Git tutorial for beginners [2024]

Git tutorial for beginners [2024]

FastAPI with SQLAlchemy Tutorial

FastAPI with SQLAlchemy Tutorial

Анализ акций с помощью чата с ИИ: LangChain и LangGraph

Анализ акций с помощью чата с ИИ: LangChain и LangGraph

LangGraph 101

LangGraph 101

Build APIs with Flask (the right way)

Build APIs with Flask (the right way)

API pagination with FastAPI and SQLAlchemy

API pagination with FastAPI and SQLAlchemy

The Windows 11 Disaster That's Killing Microsoft

The Windows 11 Disaster That's Killing Microsoft

The Man Behind Google's AI Machine | Demis Hassabis Interview

The Man Behind Google's AI Machine | Demis Hassabis Interview

FastAPI severless deployments on AWS

FastAPI severless deployments on AWS

Plotly Basic Charts

Plotly Basic Charts

The People versus Microsoft

The People versus Microsoft

Код работает в 100 раз медленнее из-за ложного разделения ресурсов.

Код работает в 100 раз медленнее из-за ложного разделения ресурсов.

I Read Honey's Source Code

I Read Honey's Source Code

Deploy with render.com tutorial (FastAPI example)

Deploy with render.com tutorial (FastAPI example)

How to Escape Google Surveillance: Replace Every Service in 2 Weeks

How to Escape Google Surveillance: Replace Every Service in 2 Weeks

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: infodtube@gmail.com