4 #ifndef CURL_HTTP_VERSION_2_0 5 #define CURL_HTTP_VERSION_2_0 CURL_HTTP_VERSION_1_1 9 std::string helpers::get_http_query(
const curl_params ¶ms)
13 for (
auto &par : params) {
14 if (result.length() > 0) {
18 result += par.first +
"=" + par.second;
32 static size_t string_write_wrapper(
void *ptr,
size_t size,
size_t nmemb, std::string *str)
34 size_t length = size * nmemb;
36 std::copy((
char *) ptr, (
char *) ptr + length, std::back_inserter(*str));
41 std::string helpers::curl_get(
const std::string &url,
43 const curl_params ¶ms,
44 const std::string &username,
45 const std::string &passwd)
48 std::string query = get_http_query(params);
49 std::string url_query = url +
"?" + query;
54 curl = curl_easy_init();
57 curl_easy_setopt(curl, CURLOPT_URL, url_query.c_str());
60 curl_easy_setopt(curl, CURLOPT_PORT, port);
63 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, string_write_wrapper);
64 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
67 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
69 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
71 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
72 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
74 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
76 if (username.length() != 0 || passwd.length() != 0) {
77 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
78 curl_easy_setopt(curl, CURLOPT_USERPWD, (username +
":" + passwd).c_str());
85 res = curl_easy_perform(curl);
88 curl_easy_cleanup(curl);
91 if (res != CURLE_OK) {
93 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
94 auto error_message =
"GET request failed to " + url_query +
". Error: (" + std::to_string(response_code) +
95 ") " + curl_easy_strerror(res);
96 throw curl_exception(error_message);
103 std::string helpers::curl_post(
const std::string &url,
105 const curl_params ¶ms,
106 const std::string &username,
107 const std::string &passwd)
110 std::string query = get_http_query(params);
111 std::string url_query = url +
"?" + query;
116 curl = curl_easy_init();
119 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
122 curl_easy_setopt(curl, CURLOPT_PORT, port);
125 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.c_str());
128 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, string_write_wrapper);
129 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
132 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
134 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
136 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
137 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
139 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
141 if (username.length() != 0 || passwd.length() != 0) {
142 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
143 curl_easy_setopt(curl, CURLOPT_USERPWD, (username +
":" + passwd).c_str());
150 res = curl_easy_perform(curl);
153 curl_easy_cleanup(curl);
156 if (res != CURLE_OK) {
158 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
159 auto error_message =
"POST request failed to " + url_query +
". Error: (" + std::to_string(response_code) +
160 ") " + curl_easy_strerror(res);
161 throw curl_exception(error_message);