[QUESTION] Add custom information to incoming Request
See original GitHub issueFirst check
- [ YES ] I used the GitHub search to find a similar issue and didn’t find it.
- [ YES ] I searched the FastAPI documentation, with the integrated search.
- [ YES ] I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
I want to run a middleware for authorization. When this middleware receives a token, It will do some processing and it will fetch some data which I want appended in the request, whether in the headers or body, So that the downstream services can use it.
How can I accomplish this ? I am trying to modify the incoming Request in the middleware, But I am not able to see my custom information in the downstream service.
This is what I am trying to do:
@app.middleware("http")
async def validate_token(request: Request, call_next):
valid_user_info = validate_token(request.headers)
# I want to append the above into the request for downstream services to use
req.headers.user_info = valid_user_info # Does not work for me
req.user_info = valid_user_info # I am unable to reference the value downstream
response = await call_next(request) # This request will be modified and sent
return response
Thanks for the help in advance!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How To Process Incoming Request Data in Flask - DigitalOcean
In Postman, add the URL and change the type to POST. On the body tab, change to raw and select JSON from the...
Read more >Incoming requests: context with custom typed fields
To use what you built, you have to use type assertions: rctx:=request.Context().(RequestContext). This will break if you have a middleware ...
Read more >Show Account name on Incoming request page and My ...
@Request Access. 01 If pamuser01 multiple select account for request access. The “system name” is from adding Customize account name on process Add...
Read more >monday forms | monday.com tutorials - YouTube
With monday forms, turn valuable data into actionable insights. ... 00:13 - Adding a form 00:26 - Customizing a form 00:52 - Question...
Read more >I Want People to Answer Questions Before I Let them Into My ...
From here you can create up to five custom questions that potential members can answer before they submit a request to join your...
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
Adding to @phy25 's comment update your middleware
req
isn’t recognized by anything, that looks like expressjs, you need to use request and do request.state adding pylint to your project should help catch undefined variableThen in your route handler you can include
request: Request
which will give you the actual request object and you can pull the variable set out of there e.g. Something like@phy25
req.state
is the way to go! Thank you so much.@chris-allnutt I was looking at fastapi through my knowledge using express js. I followed your example and things are much clearer now.
Appreciate the help guys 😃