From 087fbee1e359f40a030c70895877e9e4d4daf3fd Mon Sep 17 00:00:00 2001 From: alice <58637860+alicealys@users.noreply.github.com> Date: Mon, 27 Jul 2026 21:49:27 +0200 Subject: [PATCH] add method opt to http::request --- src/component/http.cpp | 14 ++++++++++++-- src/utils/http.cpp | 26 +++++++++++++------------- src/utils/http.hpp | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/component/http.cpp b/src/component/http.cpp index f99f06b..6f929d6 100644 --- a/src/component/http.cpp +++ b/src/component/http.cpp @@ -20,9 +20,10 @@ namespace http struct http_request_params_t { + std::string method; std::string url; std::string fields; - std::unordered_map headers; + utils::http::headers headers; }; struct http_request_t @@ -91,7 +92,10 @@ namespace http scheduler::thread_pool.push([request] { request->result = utils::http::get_data( - request->params.url, request->params.fields, request->params.headers); + request->params.url, + request->params.fields, + request->params.headers, + request->params.method); request->completed = true; }); @@ -151,6 +155,12 @@ namespace http const auto fields = options["parameters"]; const auto body = options["body"]; const auto headers = options["headers"]; + const auto method = options["method"]; + + if (method.is()) + { + params.method = method.as(); + } if (fields.is()) { diff --git a/src/utils/http.cpp b/src/utils/http.cpp index ab566b1..3cee86d 100644 --- a/src/utils/http.cpp +++ b/src/utils/http.cpp @@ -37,8 +37,7 @@ namespace utils::http size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp) { - auto* buffer = static_cast(userp); - + const auto buffer = static_cast(userp); const auto total_size = size * nmemb; buffer->append(static_cast(contents), total_size); return total_size; @@ -46,10 +45,10 @@ namespace utils::http } std::optional get_data(const std::string& url, const std::string& fields, - const headers& headers, const std::function& callback) + const headers& headers, const std::string& method) { curl_slist* header_list = nullptr; - auto* curl = curl_easy_init(); + const auto curl = curl_easy_init(); if (!curl) { return {}; @@ -68,22 +67,25 @@ namespace utils::http } std::string buffer{}; - progress_helper helper{}; - helper.callback = &callback; curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list); curl_easy_setopt(curl, CURLOPT_URL, url.data()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); - curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &helper); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); if (!fields.empty()) { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.data()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, fields.size()); } + if (!method.empty()) + { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.data()); + } + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); + const auto code = curl_easy_perform(curl); if (code == CURLE_OK) @@ -91,14 +93,12 @@ namespace utils::http result result; result.code = code; result.buffer = std::move(buffer); - return result; } else { result result; result.code = code; - return result; } } diff --git a/src/utils/http.hpp b/src/utils/http.hpp index 4fc7e4a..c2eae3f 100644 --- a/src/utils/http.hpp +++ b/src/utils/http.hpp @@ -17,5 +17,5 @@ namespace utils::http using headers = std::unordered_map; std::optional get_data(const std::string& url, const std::string& fields = {}, - const headers& headers = {}, const std::function& callback = {}); + const headers& headers = {}, const std::string& method = {}); } \ No newline at end of file