I'm trying to figure out how Extension
s are created in the actix-web Rust library. I think I'm missing the concept of request extensions. What do request extensions do? How are they different from HTTP headers?
What is the role of a "Extension" in actix-web?
Extensions are used for storing request-local data. They are different than application data since it is specific to individual requests. And is different from headers since they are set within the server, not by the client.
This is primarily used for passing data between middleware and the handlers; like authentication or route-prefix processing. They can be set in the middleware by req.extensions_mut().insert(...)
and can be retrieved in the handler via req.extensions().get<...>()
or via the ReqData
extractor.
See also:
© 2022 - 2024 — McMap. All rights reserved.