Fastapi: [QUESTION] Add custom information to incoming Request

Created on 18 Mar 2020  路  4Comments  路  Source: tiangolo/fastapi

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!

question

Most helpful comment

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)

All 4 comments

You might want to use request.state: https://www.starlette.io/requests/#other-state

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)

Thanks for the help @phy25 and @chris-allnutt ! :cake: :clap:

Does that solve your problem @subritc ? If that's the case, you can close the issue.

@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 :)

Was this page helpful?
0 / 5 - 0 ratings