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.

[QUESTION] Add custom information to incoming Request

See original GitHub issue

First 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
chris-allnuttcommented, Mar 18, 2020

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 variable

Then 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

@app.get('/route-i-wrote')
async def my_cool_route(request: Request):
  logging.info(request.state.user_info)
2reactions
subritccommented, Apr 20, 2020

@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 😃

Read more comments on GitHub >

github_iconTop 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 >

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