diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
| commit | d8c4d84dc75300c6d4d8b0adceafa33741960b92 (patch) | |
| tree | 00e2dfcc5c836d62fccff76c862e6ec3b0a74db8 /libs/cpp-httplib/example/one_time_request.cc | |
| parent | fa088bb60692ba02d30d39affa9a31d9e2b688e2 (diff) | |
added http lib, working on AI invoice importing
Diffstat (limited to 'libs/cpp-httplib/example/one_time_request.cc')
| -rw-r--r-- | libs/cpp-httplib/example/one_time_request.cc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/libs/cpp-httplib/example/one_time_request.cc b/libs/cpp-httplib/example/one_time_request.cc new file mode 100644 index 0000000..9a8ac34 --- /dev/null +++ b/libs/cpp-httplib/example/one_time_request.cc @@ -0,0 +1,56 @@ +#include <httplib.h> +#include <iostream> + +using namespace httplib; + +const char *HOST = "localhost"; +const int PORT = 1234; + +void one_time_request_server(const char *label) { + std::thread th; + Server svr; + + svr.Get("/hi", [&](const Request & /*req*/, Response &res) { + res.set_content(std::string("Hello from ") + label, "text/plain"); + + // Stop server + th = std::thread([&]() { svr.stop(); }); + }); + + svr.listen(HOST, PORT); + th.join(); + + std::cout << label << " ended..." << std::endl; +} + +void send_request(const char *label) { + Client cli(HOST, PORT); + + std::cout << "Send " << label << " request" << std::endl; + auto res = cli.Get("/hi"); + + if (res) { + std::cout << res->body << std::endl; + } else { + std::cout << "Request error: " + to_string(res.error()) << std::endl; + } +} + +int main(void) { + auto th1 = std::thread([&]() { one_time_request_server("Server #1"); }); + auto th2 = std::thread([&]() { one_time_request_server("Server #2"); }); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("1st"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("2nd"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("3rd"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + th1.join(); + th2.join(); +} |
