How can I get the request while in an after_request hook? I'm looking to create a unique cookie for each user, but I need to know if there is already a cookie in the request before adding it to the response. An after_request hook seems like the appropriate place, but I was suprised to find that it doesn't pass the request in. Is there a more appropriate place to do this? I guess I can use the server side session middleware, but I don't actually need the session, just the session_id.
#Get request from after_request hook
1 messages · Page 1 of 1 (latest)
hello @tribal robin indeed there is no request in the adter_request signature https://github.com/litestar-org/litestar/blob/1e0dc7c4d67151c836208a3e360051e983b5083a/litestar/types/callable_types.py#L23-L25 and I dont have a good answer as to why it doesnt have it, though why cant you use the before_request one in your usecase ?
If not you maybe can explore how to do that in a middleware where you'll get pretty much full control.
litestar/types/callable_types.py lines 23 to 25
AfterRequestHookHandler: TypeAlias = (
"Callable[[ASGIApp], SyncOrAsyncUnion[ASGIApp]] | Callable[[Response], SyncOrAsyncUnion[Response]]"
)