add method opt to http::request

This commit is contained in:
alice
2026-07-27 21:49:27 +02:00
parent 2e0c364ac8
commit 087fbee1e3
3 changed files with 26 additions and 16 deletions
+12 -2
View File
@@ -20,9 +20,10 @@ namespace http
struct http_request_params_t struct http_request_params_t
{ {
std::string method;
std::string url; std::string url;
std::string fields; std::string fields;
std::unordered_map<std::string, std::string> headers; utils::http::headers headers;
}; };
struct http_request_t struct http_request_t
@@ -91,7 +92,10 @@ namespace http
scheduler::thread_pool.push([request] scheduler::thread_pool.push([request]
{ {
request->result = utils::http::get_data( 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; request->completed = true;
}); });
@@ -151,6 +155,12 @@ namespace http
const auto fields = options["parameters"]; const auto fields = options["parameters"];
const auto body = options["body"]; const auto body = options["body"];
const auto headers = options["headers"]; const auto headers = options["headers"];
const auto method = options["method"];
if (method.is<std::string>())
{
params.method = method.as<std::string>();
}
if (fields.is<scripting::array>()) if (fields.is<scripting::array>())
{ {
+13 -13
View File
@@ -37,8 +37,7 @@ namespace utils::http
size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp) size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp)
{ {
auto* buffer = static_cast<std::string*>(userp); const auto buffer = static_cast<std::string*>(userp);
const auto total_size = size * nmemb; const auto total_size = size * nmemb;
buffer->append(static_cast<char*>(contents), total_size); buffer->append(static_cast<char*>(contents), total_size);
return total_size; return total_size;
@@ -46,10 +45,10 @@ namespace utils::http
} }
std::optional<result> get_data(const std::string& url, const std::string& fields, std::optional<result> get_data(const std::string& url, const std::string& fields,
const headers& headers, const std::function<void(size_t)>& callback) const headers& headers, const std::string& method)
{ {
curl_slist* header_list = nullptr; curl_slist* header_list = nullptr;
auto* curl = curl_easy_init(); const auto curl = curl_easy_init();
if (!curl) if (!curl)
{ {
return {}; return {};
@@ -68,22 +67,25 @@ namespace utils::http
} }
std::string buffer{}; std::string buffer{};
progress_helper helper{};
helper.callback = &callback;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_URL, url.data()); 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()) if (!fields.empty())
{ {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.data()); 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); const auto code = curl_easy_perform(curl);
if (code == CURLE_OK) if (code == CURLE_OK)
@@ -91,14 +93,12 @@ namespace utils::http
result result; result result;
result.code = code; result.code = code;
result.buffer = std::move(buffer); result.buffer = std::move(buffer);
return result; return result;
} }
else else
{ {
result result; result result;
result.code = code; result.code = code;
return result; return result;
} }
} }
+1 -1
View File
@@ -17,5 +17,5 @@ namespace utils::http
using headers = std::unordered_map<std::string, std::string>; using headers = std::unordered_map<std::string, std::string>;
std::optional<result> get_data(const std::string& url, const std::string& fields = {}, std::optional<result> get_data(const std::string& url, const std::string& fields = {},
const headers& headers = {}, const std::function<void(size_t)>& callback = {}); const headers& headers = {}, const std::string& method = {});
} }