This commit is contained in:
yhirose
2026-04-03 18:27:12 -04:00
parent 6607a6a592
commit 3093bdd9ab
5 changed files with 90 additions and 156 deletions

View File

@@ -164,13 +164,13 @@ Use `res.user_data` to pass data from middleware to handlers. This is useful for
```cpp
svr.set_pre_routing_handler([](const auto &req, auto &res) {
res.user_data["auth_user"] = std::string("alice");
res.user_data.set("auth_user", std::string("alice"));
return httplib::Server::HandlerResponse::Unhandled;
});
svr.Get("/me", [](const auto &req, auto &res) {
auto user = std::any_cast<std::string>(res.user_data.at("auth_user"));
res.set_content("Hello, " + user, "text/plain");
auto *user = res.user_data.get<std::string>("auth_user");
res.set_content("Hello, " + *user, "text/plain");
});
```