The MbContextStore class provides a key-value store for scripting and internal utilities. HTTP handlers should use MbRequest::set and MbRequest::get for per-request attributes (auth, etc.).
For instance, the auth middleware will inject user id and subsequent middlewares can retrieve it as needed.
Context ctx;
ctx.set<std::string>("key", "Value");
ctx.set<int>("id", 967567);
ctx.set<bool>("verified", true);
req.set("key", 5") // INT/DOUBLE/FLOATS ...
req.set("key2", { a: 5, b: 7}) // Objects/JSON
req.set("valid", true) // BOOLs
req.get("nothing", "Default Value")
The value returned from the get() is a std::optional, meaning a std::nullopt if the key was not found.
std::optional key = ctx.get<std::string>("key");
if(key.has_value()) { .... }
Additionally, we have an overload of
- See also
- get() that takes in a key and a default value if the key is missing. This unlike the optional-returning
-
get() method, returns a T& instead of T* depending on the usage needs.