This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request.session in HTTP Middleware #4746
Comments
I was able to find this in the Starlette docs: It looks like this package might provide the functionality you're looking for, though! |
Perhaps you could use this |
@wmcgee3 tried all and going with request.state for now :) |
This may help: order of middleware |
I am still receiving this even just reading the session variable: @app.middleware("http")
async def validate_user(request: Request, call_next):
end_point = request.url.path
print(request.session) # <-- AssertionError: SessionMiddleware must be installed to access request.session
response = await call_next(request)
return response How were you able to end up reading this? |
You need to make sure your middleware is attached to the app FIRST. They are executed in reverse order - what is happening is that session hasn't run yet, so there is no request.session attribute (yet). Get the middleware in the correct order, and you will always run AFTER the session middleware, and the problem goes away. |
@janczawadzki from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse, PlainTextResponse
from starlette.middleware.sessions import SessionMiddleware
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="some-random-string")
@app.middleware("http")
async def validate_user(request: Request, call_next):
print(request.session) # <--- Error: 'AssertionError: SessionMiddleware must be installed to access request.session'
response = await call_next(request)
return response
@app.get("/set/{item}")
async def set(request: Request, item:str):
request.session["item"] = item
return request.session["item"]
@app.get("/get")
async def get(request: Request) -> PlainTextResponse:
return request.session.get("item", None) Am i doing something obviously wrong here? |
In order to fix this please move: |
HA! That worked but is less than intuitive.... :) |
@sagar-arya can you post your working code with fastapi-session? |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
This is a basic example of my app.
I am trying to add values into the session if the user is valid.
For now, I am trying to set
In the process, I get SessionMiddleware must be installed to access request.session
Is there any other way for solving it?
I am able to set it using
I am not sure if this is a valid way to do it or not.
Can someone please help me with this?
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.74.1
Python Version
3.10.2
Additional Context
No response
The text was updated successfully, but these errors were encountered: