What is the role of a "Extension" in actix-web?
Asked Answered
F

1

6

I'm trying to figure out how Extensions 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?

Flair answered 31/8, 2021 at 4:11 Comment(0)
T
12

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:

Tol answered 31/8, 2021 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.