mirror of
https://github.com/alicealys/t5-gsc-utils.git
synced 2026-07-28 10:40:36 +00:00
add method opt to http::request
This commit is contained in:
+12
-2
@@ -20,9 +20,10 @@ namespace http
|
||||
|
||||
struct http_request_params_t
|
||||
{
|
||||
std::string method;
|
||||
std::string url;
|
||||
std::string fields;
|
||||
std::unordered_map<std::string, std::string> 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<std::string>())
|
||||
{
|
||||
params.method = method.as<std::string>();
|
||||
}
|
||||
|
||||
if (fields.is<scripting::array>())
|
||||
{
|
||||
|
||||
+13
-13
@@ -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<std::string*>(userp);
|
||||
|
||||
const auto buffer = static_cast<std::string*>(userp);
|
||||
const auto total_size = size * nmemb;
|
||||
buffer->append(static_cast<char*>(contents), 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,
|
||||
const headers& headers, const std::function<void(size_t)>& 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;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,5 +17,5 @@ namespace utils::http
|
||||
using headers = std::unordered_map<std::string, std::string>;
|
||||
|
||||
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 = {});
|
||||
}
|
||||
Reference in New Issue
Block a user