tomwojcik/starlette-context

Access context from another middleware

HarrySky opened this issue ยท 2 comments

Hi, thanks for this great middleware! ๐Ÿ˜„
I was trying to access context from another middleware and was getting a lot of RuntimeErrors saying I was not in request-response cycle.

Problem is solved, I am leaving this issue in case somebody else have this "access context in another middleware" use-case and is getting RuntimeErrors. You should add RawContextMiddleware last (see: encode/starlette#479):

app = Starlette()  # or FastAPI()

# Add your middleware that uses context here...

app.add_middleware(RawContextMiddleware)

It would be great if you could add a warning about this in docs' examples, but I don't insist as it is actually more of a Starlette problem

Hey, thanks!

Proper order of middlewares is important in all web apps. It's not a Starlette problem, it's just what it is.

I don't like hanging tickets. I was sure I mentioned the proper order of middlewares in the docs but can't find it anywhere so I'll do that.

Thanks for pointing that out!