Following Starlette documentation (FastAPI uses Starlette for middlewares), response.headers["Authorization"]
should allow me to get the bearer token, but I get a KeyError
saying no such attribute exists.
When I print response.headers
, I get MutableHeaders({'content-length': '14', 'content-type': 'application/json'})
.
Why is the authorization attribute not in the header despite of making a request with an auth
header?
@app.middleware("http")
async def validate_access_token(request: Request, call_next):
response = await call_next(request)
access_token = response.headers["Authorization"].split()
is_valid_signature = jwt.decode(access_token[1], key=SECRET, algorithms=CRYPT_ALGO)
if is_valid_signature:
return response
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Invalid access token'
)