question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass and access the parameters inside middleware? Which passing technique to use, query parameter or path parameter?

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI, Request

app = FastAPI()

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

Description

Assume that, I Should pass one parameter(eg: query) to the middleware (add_process_time_header), How should I pass the parameter, and how to access it inside the middleware?

Operating System

Linux

Operating System Details

No response

FastAPI Version

Flask-API==2.0

Python Version

3.6.9

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
pradeepdev-1995commented, Mar 30, 2022

@includeamin Thank you.

1reaction
includeamincommented, Mar 29, 2022

Just a quick example to give you an idea.

import time

from fastapi import FastAPI, Request

app = FastAPI()


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    if request.query_params.__contains__("set-process-time"):
        response.headers["X-Process-Time"] = str(process_time)
    return response


@app.get("/")
async def root():
    return {"message": "Hello World"}

URL: http://127.0.0.1:8000/?set-process-time=1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Laravel Route Parameters in Middleware - DigitalOcean
Method 1: $request->route('parameter_name') ... We can access route parameters in two ways. One way is by using $request->route('parameter_name') ...
Read more >
Add query parameter in middleware - Laracasts
For a demo mode (used in an iframe) I open my website with a query parameter, e.g. ?demo=true I use this in order...
Read more >
How to pass parameters to action methods in ASP.NET Core ...
Passing parameters in the query string is another option. It doesn't require changing the routing information and hence is backwards compatible.
Read more >
How to pass variables to the next middleware using next() in ...
The following example covers how to pass variables to the next middleware using next() in Express. js. Approach: We cannot directly pass data ......
Read more >
Get Query Strings and Parameters in Express.js - Stack Abuse
Before we start, it'd be helpful to first understand what exactly a query string/parameter is, and then we'll talk about how we can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found