<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[DevNode]]></title><description><![CDATA[DevNode]]></description><link>https://blog.dumidu.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1751427116233/cccd92af-a6c0-4c58-bff7-d6a81c644917.png</url><title>DevNode</title><link>https://blog.dumidu.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 08:11:36 GMT</lastBuildDate><atom:link href="https://blog.dumidu.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to use middleware's in Fastapi app]]></title><description><![CDATA[A "middleware" is a function that works with every request before it is processed by any specific path operation. And also with every response before returning it.

It takes each request that comes to your application.

It can then do something to th...]]></description><link>https://blog.dumidu.dev/how-to-use-middlewares-in-fastapi-app</link><guid isPermaLink="true">https://blog.dumidu.dev/how-to-use-middlewares-in-fastapi-app</guid><category><![CDATA[FastAPI]]></category><category><![CDATA[Middleware]]></category><dc:creator><![CDATA[Dumidu Lakshan]]></dc:creator><pubDate>Wed, 02 Jul 2025 03:43:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751427471466/bcd5c2f8-b034-4ad1-a686-443ee7dfd9b8.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-a-middleware-is-a-function-that-works-with-every-request-before-it-is-processed-by-any-specific-path-operation-and-also-with-every-response-before-returning-it">A <code>"middleware"</code> is a function that works with every <strong>request</strong> before it is processed by any specific <em>path operation</em>. And also with every <strong>response</strong> before returning it.</h2>
<ul>
<li><p>It takes each <strong>request</strong> that comes to your application.</p>
</li>
<li><p>It can then do something to that <strong>request</strong> or run any needed code.</p>
</li>
<li><p>Then it passes the <strong>request</strong> to be processed by the rest of the application (by some <em>path operation</em>).</p>
</li>
<li><p>It then takes the <strong>response</strong> generated by the application (by some <em>path operation</em>).</p>
</li>
<li><p>It can do something to that <strong>response</strong> or run any needed code.</p>
</li>
</ul>
<p>Then it returns the <strong>response</strong>.</p>
<hr />
<h2 id="heading-create-a-middleware">Create a middleware</h2>
<p>To create a middleware you use the decorator <code>@app.middleware("http")</code> on top of a function.</p>
<p>The middleware function receives:</p>
<ul>
<li><p>The <code>request</code>.</p>
</li>
<li><p>A function <code>call_next</code> that will receive the <code>request</code> as a parameter.</p>
<ul>
<li><p>This function will pass the <code>request</code> to the corresponding <em>path operation</em>.</p>
</li>
<li><p>Then it returns the <code>response</code> generated by the corresponding <em>path operation</em>.</p>
</li>
</ul>
</li>
<li><p>You can then further modify the <code>response</code> before returning it.</p>
</li>
</ul>
<hr />
<pre><code class="lang-python"><span class="hljs-keyword">import</span> time

<span class="hljs-keyword">from</span> fastapi <span class="hljs-keyword">import</span> FastAPI, Request

app = FastAPI()


<span class="hljs-meta">@app.middleware("http")</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add_process_time_header</span>(<span class="hljs-params">request: Request, call_next</span>):</span>
    start_time = time.perf_counter()
    response = <span class="hljs-keyword">await</span> call_next(request)
    process_time = time.perf_counter() - start_time
    response.headers[<span class="hljs-string">"X-Process-Time"</span>] = str(process_time)
    <span class="hljs-keyword">return</span> response
</code></pre>
]]></content:encoded></item></channel></rss>