How to pass and access the parameters inside middleware? Which passing technique to use, query parameter or path parameter?
See original GitHub issueFirst 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:
- Created a year ago
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@includeamin Thank you.
Just a quick example to give you an idea.
URL: http://127.0.0.1:8000/?set-process-time=1