|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <iostream> |
| 22 | +#include <memory> |
| 23 | +#include <sstream> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +#include <httpserver.hpp> |
| 27 | + |
| 28 | +// This example demonstrates how to use get_args() and get_args_flat() to |
| 29 | +// process all query string and body arguments from an HTTP request. |
| 30 | +// |
| 31 | +// Try these URLs: |
| 32 | +// http://localhost:8080/args?name=john&age=30 |
| 33 | +// http://localhost:8080/args?id=1&id=2&id=3 (multiple values for same key) |
| 34 | +// http://localhost:8080/args?colors=red&colors=green&colors=blue |
| 35 | + |
| 36 | +class args_resource : public httpserver::http_resource { |
| 37 | + public: |
| 38 | + std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req) { |
| 39 | + std::stringstream response_body; |
| 40 | + |
| 41 | + response_body << "=== Using get_args() (supports multiple values per key) ===\n\n"; |
| 42 | + |
| 43 | + // get_args() returns a map where each key maps to an http_arg_value. |
| 44 | + // http_arg_value contains a vector of values for parameters like "?id=1&id=2&id=3" |
| 45 | + auto args = req.get_args(); |
| 46 | + for (const auto& [key, arg_value] : args) { |
| 47 | + response_body << "Key: " << key << "\n"; |
| 48 | + // Use get_all_values() to get all values for this key |
| 49 | + auto all_values = arg_value.get_all_values(); |
| 50 | + if (all_values.size() > 1) { |
| 51 | + response_body << " Values (" << all_values.size() << "):\n"; |
| 52 | + for (const auto& v : all_values) { |
| 53 | + response_body << " - " << v << "\n"; |
| 54 | + } |
| 55 | + } else { |
| 56 | + // For single values, http_arg_value converts to string_view |
| 57 | + response_body << " Value: " << std::string_view(arg_value) << "\n"; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + response_body << "\n=== Using get_args_flat() (one value per key) ===\n\n"; |
| 62 | + |
| 63 | + // get_args_flat() returns a simple map with one value per key. |
| 64 | + // If a key has multiple values, only the first value is returned. |
| 65 | + auto args_flat = req.get_args_flat(); |
| 66 | + for (const auto& [key, value] : args_flat) { |
| 67 | + response_body << key << " = " << value << "\n"; |
| 68 | + } |
| 69 | + |
| 70 | + response_body << "\n=== Accessing individual arguments ===\n\n"; |
| 71 | + |
| 72 | + // You can also access individual arguments directly |
| 73 | + auto name = req.get_arg("name"); // Returns http_arg_value (may have multiple values) |
| 74 | + auto name_flat = req.get_arg_flat("name"); // Returns string_view (first value only) |
| 75 | + |
| 76 | + if (!name.get_flat_value().empty()) { |
| 77 | + response_body << "name (via get_arg): " << std::string_view(name) << "\n"; |
| 78 | + } |
| 79 | + if (!name_flat.empty()) { |
| 80 | + response_body << "name (via get_arg_flat): " << name_flat << "\n"; |
| 81 | + } |
| 82 | + |
| 83 | + return std::make_shared<httpserver::string_response>(response_body.str(), 200, "text/plain"); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +int main() { |
| 88 | + httpserver::webserver ws = httpserver::create_webserver(8080); |
| 89 | + |
| 90 | + args_resource ar; |
| 91 | + ws.register_resource("/args", &ar); |
| 92 | + |
| 93 | + std::cout << "Server running on http://localhost:8080/args\n"; |
| 94 | + std::cout << "Try: http://localhost:8080/args?name=john&age=30\n"; |
| 95 | + std::cout << "Or: http://localhost:8080/args?id=1&id=2&id=3\n"; |
| 96 | + |
| 97 | + ws.start(true); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
0 commit comments