diff options
Diffstat (limited to 'examples/example.cpp')
-rw-r--r-- | examples/example.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/examples/example.cpp b/examples/example.cpp index dec57cd..87231a2 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -1,11 +1,10 @@ #include "crow.h" -#include "json.h" #include <sstream> class ExampleLogHandler : public crow::ILogHandler { public: - void log(std::string message, crow::LogLevel level) override { + void log(std::string /*message*/, crow::LogLevel /*level*/) override { // cerr << "ExampleLogHandler -> " << message; } }; @@ -28,12 +27,12 @@ struct ExampleMiddleware { }; - void before_handle(crow::request& req, crow::response& res, context& ctx) + void before_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/) { CROW_LOG_DEBUG << " - MESSAGE: " << message; } - void after_handle(crow::request& req, crow::response& res, context& ctx) + void after_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/) { // no-op } @@ -85,7 +84,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){ + ([](const crow::request& /*req*/, crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); @@ -106,6 +105,9 @@ int main() // * Select 'raw' and then JSON // * Add {"a": 1, "b": 1} // * Send and you should receive 2 + + // A simpler way for json example: + // * curl -d '{"a":1,"b":2}' {ip}:18080/add_json CROW_ROUTE(app, "/add_json") .methods("POST"_method) ([](const crow::request& req){ @@ -144,9 +146,23 @@ int main() for(const auto& countVal : count) { os << " - " << countVal << '\n'; } + + // To get a dictionary from the request + // You have to submit something like '/params?mydict[a]=b&mydict[abcd]=42' to have a list of pairs ((a, b) and (abcd, 42)) + auto mydict = req.url_params.get_dict("mydict"); + os << "The key 'dict' contains " << mydict.size() << " value(s).\n"; + for(const auto& mydictVal : mydict) { + os << " - " << mydictVal.first << " -> " << mydictVal.second << '\n'; + } + return crow::response{os.str()}; }); + CROW_ROUTE(app, "/large") + ([]{ + return std::string(512*1024, ' '); + }); + // ignore all log crow::logger::setLogLevel(crow::LogLevel::DEBUG); //crow::logger::setHandler(std::make_shared<ExampleLogHandler>()); |