GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — libhttpserver/examples/args_processing.cpp at master · etr/libhttpserver Expand file tree/ Copy pathargs_processing.cppMore file actions 95 lines (77 loc) · 3.83 KB / Copy pathargs_processing.cppFile metadata and controls95 lines (77 loc) · 3.83 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 /* This file is part of libhttpserver Copyright (C) 2011-2025 Sebastiano Merlino This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ // args_processing.cpp - demonstrates get_args() and get_args_flat() to // process all query string and body arguments from an HTTP request. // // Try these URLs: // http://localhost:8080/args?name=john&age=30 // http://localhost:8080/args?id=1&id=2&id=3 (multiple values for same key) // http://localhost:8080/args?colors=red&colors=green&colors=blue #include <iostream> #include <sstream> #include <string> #include <string_view> #include <httpserver.hpp> int main() { httpserver::webserver ws{httpserver::create_webserver(8080)}; ws.on_get("/args", [](const httpserver::http_request& req) { std::ostringstream response_body; response_body << "=== Using get_args() (supports multiple values per key) ===\n\n"; // get_args() returns a const reference to a map where each key // maps to an http_arg_value. http_arg_value contains a vector of // values for parameters like "?id=1&id=2&id=3". The reference // remains valid for the duration of this handler call. const auto& args = req.get_args(); for (const auto& [key, arg_value] : args) { response_body << "Key: " << key << "\n"; // Use get_all_values() to get all values for this key auto all_values = arg_value.get_all_values(); if (all_values.size() > 1) { response_body << " Values (" << all_values.size() << "):\n"; for (const auto& v : all_values) { response_body << " - " << v << "\n"; } } else { // For single values, http_arg_value converts to string_view response_body << " Value: " << std::string_view(arg_value) << "\n"; } } response_body << "\n=== Using get_args_flat() (one value per key) ===\n\n"; // get_args_flat() returns a simple map with one value per key. // If a key has multiple values, only the first value is returned. auto args_flat = req.get_args_flat(); for (const auto& [key, value] : args_flat) { response_body << key << " = " << value << "\n"; } response_body << "\n=== Accessing individual arguments ===\n\n"; // You can also access individual arguments directly auto name = req.get_arg("name"); // Returns http_arg_value (may have multiple values) auto name_flat = req.get_arg_flat("name"); // Returns string_view (first value only) if (!name.get_flat_value().empty()) { response_body << "name (via get_arg): " << std::string_view(name) << "\n"; } if (!name_flat.empty()) { response_body << "name (via get_arg_flat): " << name_flat << "\n"; } return httpserver::http_response::string(response_body.str()); }); std::cout << "Server running on http://localhost:8080/args\n"; std::cout << "Try: http://localhost:8080/args?name=john&age=30\n"; std::cout << "Or: http://localhost:8080/args?id=1&id=2&id=3\n"; ws.start(true); return 0; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |