diff -r 4f96098f7c57 -r dbeae485a3fd src/HTTPClient.cpp --- a/src/HTTPClient.cpp Mon Apr 04 23:07:31 2022 +0200 +++ b/src/HTTPClient.cpp Tue Apr 05 00:06:12 2022 +0200 @@ -32,6 +32,7 @@ public: CURL* curl; char curlErrorBuffer[CURL_ERROR_SIZE]; + std::stringstream requestBody; std::stringstream responseBody; std::stringstream responseHeaders; @@ -93,8 +94,17 @@ HTTPClient* HTTPClient::open() { HTTPClient::HTTPClientImpl* impl = new HTTPClient::HTTPClientImpl(curl_easy_init()); + typedef size_t(*CurlReadCallback)(char*, size_t, size_t, HTTPClient::HTTPClientImpl*); typedef size_t(*CurlWriteCallback)(char*, size_t, size_t, HTTPClient::HTTPClientImpl*); + // set request body callback + curl_easy_setopt(impl->curl, CURLOPT_READDATA, impl); + curl_easy_setopt(impl->curl, CURLOPT_READFUNCTION, (CurlReadCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl) { + size_t r = size * nmemb; + ssize_t bytesRead = impl->requestBody.readsome(buffer, r); + return (size_t) bytesRead; + }); + // set response body callback curl_easy_setopt(impl->curl, CURLOPT_WRITEDATA, impl); curl_easy_setopt(impl->curl, CURLOPT_WRITEFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t { @@ -156,6 +166,14 @@ for (size_t i = 0; i < request.headers.size(); i += 2) requestHeders.append(request.headers[i] + ": " + request.headers[i + 1]); // TODO: validate, no CR/LF... curl_easy_setopt(impl->curl, CURLOPT_HTTPHEADER, requestHeders.getList()); + // store request body + if (request.body.size()) { + impl->requestBody = std::stringstream(request.body); + curl_easy_setopt(impl->curl, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(impl->curl, CURLOPT_EXPECT_100_TIMEOUT_MS, 0); + } + + // clear the error buffer impl->curlErrorBuffer[0] = 0;