From 201f3665b3a26fac72aa3f47de513f1b97860492 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 12 Jun 2023 20:19:37 +0200 Subject: [PATCH] fix restclient corrupting requests --- include/restclient.h | 2 ++ src/restclient.cpp | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/include/restclient.h b/include/restclient.h index 4a89f2e..52b7abc 100644 --- a/include/restclient.h +++ b/include/restclient.h @@ -46,3 +46,5 @@ protected: bool disableCertCheck = false; #endif }; + +const char* toString(RestClient::Method); diff --git a/src/restclient.cpp b/src/restclient.cpp index c14ea12..212a71a 100644 --- a/src/restclient.cpp +++ b/src/restclient.cpp @@ -81,9 +81,11 @@ QByteArray RestClient::requestRaw(const QString& url, Method m, const QString& d if (!curl) return {}; QString requrl = baseurl + url; - curl_easy_setopt(curl, CURLOPT_URL, requrl.toLocal8Bit().data()); + const QByteArray reqArr = requrl.toLocal8Bit(); + curl_easy_setopt(curl, CURLOPT_URL, reqArr.data()); - curl_easy_setopt(curl, CURLOPT_USERPWD, basicauth.toLocal8Bit().data()); + const QByteArray basicArr = basicauth.toLocal8Bit(); + curl_easy_setopt(curl, CURLOPT_USERPWD, basicArr.data()); curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); if (disableCertCheck) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); @@ -99,6 +101,7 @@ QByteArray RestClient::requestRaw(const QString& url, Method m, const QString& d struct curl_slist* headerlist = NULL; headerlist = curl_slist_append(headerlist, "Accept: application/json"); + const QByteArray dataArr = data.toLocal8Bit(); switch (m) { default: @@ -106,22 +109,14 @@ QByteArray RestClient::requestRaw(const QString& url, Method m, const QString& d curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); break; case Method::POST: curl_easy_setopt(curl, CURLOPT_POST, 1L); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataArr.data()); headerlist = curl_slist_append(headerlist, "Content-Type: application/json"); break; case Method::PUT: - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); // to use the POSTFIELDS (do not use CURLOPT_PUT, it does not support POSTFIELDS) - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data()); - headerlist = curl_slist_append(headerlist, "Content-Type: application/json"); - break; case Method::PATCH: - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data()); - headerlist = curl_slist_append(headerlist, "Content-Type: application/json"); - break; case Method::DELETE: - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data()); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, toString(m)); // to use the POSTFIELDS (do not use CURLOPT_PUT, it does not support POSTFIELDS) + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataArr.data()); headerlist = curl_slist_append(headerlist, "Content-Type: application/json"); break; } @@ -184,3 +179,9 @@ QString RestClient::escape(const QString& in) const { curl_free(e); return esc; } + +const char* toString(RestClient::Method m) { + static const char* MethodNames[] = {"GET", "POST", "PUT", "PATCH", "DELETE"}; + + return MethodNames[(int) m]; +}