mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
Merge pull request #76 from ayaankhan98/master
recieve response without request being another argument in handler
This commit is contained in:
commit
c81a2d84e4
@ -30,8 +30,7 @@ You can also access the url parameters in the handler using `#!cpp req.url_param
|
||||
For more information on `crow::request` go [here](../../reference/structcrow_1_1request.html).<br><br>
|
||||
|
||||
###Response
|
||||
Crow also provides the ability to define a response in the parameters by using `#!cpp ([](const crow::request& req, crow::response& res){...})`.<br>
|
||||
If you don't want to use the request you can write `#!cpp ([](const crow::request& , crow::response& res){...})`.<br><br>
|
||||
Crow also provides the ability to define a response in the parameters by using `#!cpp ([](crow::response& res){...})`.<br><br>
|
||||
|
||||
Please note that in order to return a response defined as a parameter you'll need to use `res.end();`.<br><br>
|
||||
|
||||
|
@ -13,12 +13,11 @@ struct ExampleMiddleware
|
||||
{
|
||||
std::string message;
|
||||
|
||||
ExampleMiddleware()
|
||||
ExampleMiddleware() : message("foo")
|
||||
{
|
||||
message = "foo";
|
||||
}
|
||||
|
||||
void setMessage(std::string newMsg)
|
||||
void setMessage(const std::string &newMsg)
|
||||
{
|
||||
message = newMsg;
|
||||
}
|
||||
@ -84,7 +83,7 @@ int main()
|
||||
|
||||
// To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
|
||||
CROW_ROUTE(app,"/add/<int>/<int>")
|
||||
([](const crow::request& /*req*/, crow::response& res, int a, int b){
|
||||
([](crow::response& res, int a, int b){
|
||||
std::ostringstream os;
|
||||
os << a+b;
|
||||
res.write(os.str());
|
||||
|
@ -13,12 +13,11 @@ struct ExampleMiddleware
|
||||
{
|
||||
std::string message;
|
||||
|
||||
ExampleMiddleware()
|
||||
ExampleMiddleware() : message("foo")
|
||||
{
|
||||
message = "foo";
|
||||
}
|
||||
|
||||
void setMessage(std::string newMsg)
|
||||
void setMessage(const std::string &newMsg)
|
||||
{
|
||||
message = newMsg;
|
||||
}
|
||||
@ -78,7 +77,7 @@ int main()
|
||||
});
|
||||
|
||||
app.route_dynamic("/add/<int>/<int>")
|
||||
([](const crow::request& req, crow::response& res, int a, int b){
|
||||
([](crow::response& res, int a, int b){
|
||||
std::ostringstream os;
|
||||
os << a+b;
|
||||
res.write(os.str());
|
||||
|
@ -41,8 +41,10 @@ int main()
|
||||
return crow::response(os.str());
|
||||
});
|
||||
|
||||
// example which uses only response as a paramter wihtout
|
||||
// request being a parameter.
|
||||
CROW_ROUTE(app,"/add/<int>/<int>")
|
||||
([](const crow::request& /*req*/, crow::response& res, int a, int b){
|
||||
([](crow::response& res, int a, int b){
|
||||
std::ostringstream os;
|
||||
os << a+b;
|
||||
res.write(os.str());
|
||||
|
@ -529,7 +529,33 @@ namespace crow
|
||||
template <typename Func>
|
||||
typename std::enable_if<
|
||||
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
|
||||
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value,
|
||||
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
|
||||
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
|
||||
void>::type
|
||||
operator()(Func&& f)
|
||||
{
|
||||
static_assert(black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
|
||||
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value
|
||||
,
|
||||
"Handler type is mismatched with URL parameters");
|
||||
static_assert(std::is_same<void, decltype(f(std::declval<crow::response&>(), std::declval<Args>()...))>::value,
|
||||
"Handler function with response argument should have void return type");
|
||||
handler_ = (
|
||||
#ifdef CROW_CAN_USE_CPP14
|
||||
[f = std::move(f)]
|
||||
#else
|
||||
[f]
|
||||
#endif
|
||||
(const crow::request&, crow::response& res, Args ... args){
|
||||
f(res, args...);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Func>
|
||||
typename std::enable_if<
|
||||
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
|
||||
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
|
||||
!black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
|
||||
void>::type
|
||||
operator()(Func&& f)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user