diff --git a/libs/http_client/pihttpclient.cpp b/libs/http_client/pihttpclient.cpp index 42d5c0ca..2429f08f 100644 --- a/libs/http_client/pihttpclient.cpp +++ b/libs/http_client/pihttpclient.cpp @@ -22,7 +22,8 @@ int debug_callback(CURL * handle, curl_infotype type, char * data, size_t size, PRIVATE_DEFINITION_START(PIHTTPClient) - CURL * handle = nullptr; + CURL * handle = nullptr; + curl_slist * header_list = nullptr; PRIVATE_DEFINITION_END(PIHTTPClient) @@ -46,6 +47,13 @@ bool PIHTTPClient::init() { url.append('='); url.append(ait.value().toPercentageEncoding()); } + headers.clear(); + auto hit = request.headers().makeIterator(); + while (hit.next()) { + headers << hit.key() + ": " + hit.value(); + } + for (const auto & h: headers) + PRIVATE->header_list = curl_slist_append(PRIVATE->header_list, h.dataAscii()); curl_easy_setopt(PRIVATE->handle, CURLOPT_WRITEDATA, this); curl_easy_setopt(PRIVATE->handle, CURLOPT_READDATA, this); curl_easy_setopt(PRIVATE->handle, CURLOPT_XFERINFODATA, this); @@ -58,6 +66,7 @@ bool PIHTTPClient::init() { curl_easy_setopt(PRIVATE->handle, CURLOPT_HEADERFUNCTION, headerFunc); curl_easy_setopt(PRIVATE->handle, CURLOPT_URL, url.dataUTF8()); curl_easy_setopt(PRIVATE->handle, CURLOPT_CUSTOMREQUEST, PIHTTP::methodName(request.method())); + curl_easy_setopt(PRIVATE->handle, CURLOPT_HTTPHEADER, PRIVATE->header_list); curl_easy_setopt(PRIVATE->handle, CURLOPT_NOPROGRESS, 0L); // curl_easy_setopt(PRIVATE->handle, CURLOPT_VERBOSE, 1L); // curl_easy_setopt(PRIVATE->handle, CURLOPT_ERRORBUFFER, buffer_error.data()); @@ -66,6 +75,7 @@ bool PIHTTPClient::init() { curl_easy_setopt(PRIVATE->handle, CURLOPT_UPLOAD, 1L); curl_easy_setopt(PRIVATE->handle, CURLOPT_INFILESIZE_LARGE, static_cast(request.body().size())); } + return true; } @@ -91,8 +101,10 @@ void PIHTTPClient::perform() { } } // piCout << last_error; + if (PRIVATE->header_list) curl_slist_free_all(PRIVATE->header_list); curl_easy_cleanup(PRIVATE->handle); - PRIVATE->handle = nullptr; + PRIVATE->handle = nullptr; + PRIVATE->header_list = nullptr; } diff --git a/libs/main/http_client/pihttpclient.h b/libs/main/http_client/pihttpclient.h index 08d56042..ed270d51 100644 --- a/libs/main/http_client/pihttpclient.h +++ b/libs/main/http_client/pihttpclient.h @@ -3,6 +3,7 @@ #include "pihttptypes.h" #include "pip_http_client_export.h" +#include "pistringlist.h" class PIHTTPClientBase { @@ -50,6 +51,7 @@ private: PIString url; PIString last_error; + PIStringList headers; PIByteArray buffer_out; PIHTTP::MessageMutable request, reply; std::atomic_bool is_cancel = {false};