Skip to content

Commit 34894cd

Browse files
author
Karol Samborski
committed
Allow for setting additional HTTP headers
1 parent 67a0ff4 commit 34894cd

2 files changed

Lines changed: 117 additions & 9 deletions

File tree

include/restclient-cpp/restclient.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,20 @@ class RestClient
4242
static void setAuth(const std::string& user,const std::string& password);
4343
// HTTP GET
4444
static response get(const std::string& url);
45+
static response get(const std::string& url, const headermap& headers);
4546
// HTTP POST
4647
static response post(const std::string& url, const std::string& ctype,
4748
const std::string& data);
49+
static response post(const std::string& url, const std::string& ctype,
50+
const std::string& data, const headermap& headers);
4851
// HTTP PUT
4952
static response put(const std::string& url, const std::string& ctype,
5053
const std::string& data);
54+
static response put(const std::string& url, const std::string& ctype,
55+
const std::string& data, const headermap& headers);
5156
// HTTP DELETE
5257
static response del(const std::string& url);
58+
static response del(const std::string& url, const headermap& headers);
5359

5460
private:
5561
// writedata callback function

source/restclient.cpp

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,28 @@ void RestClient::setAuth(const std::string& user,const std::string& password){
3636
* @return response struct
3737
*/
3838
RestClient::response RestClient::get(const std::string& url)
39+
{
40+
headermap emptyMap;
41+
return RestClient::get(url, emptyMap);
42+
}
43+
44+
/**
45+
* @brief HTTP GET method
46+
*
47+
* @param url to query
48+
* @param headers HTTP headers
49+
*
50+
* @return response struct
51+
*/
52+
RestClient::response RestClient::get(const std::string& url, const headermap& headers)
3953
{
4054
/** create return struct */
4155
RestClient::response ret = {};
4256

4357
// use libcurl
4458
CURL *curl = NULL;
4559
CURLcode res = CURLE_OK;
60+
std::string header;
4661

4762
curl = curl_easy_init();
4863
if (curl)
@@ -64,6 +79,15 @@ RestClient::response RestClient::get(const std::string& url)
6479
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
6580
/** callback object for headers */
6681
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
82+
/** set http headers */
83+
curl_slist* hlist = NULL;
84+
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
85+
header = it->first;
86+
header += ':';
87+
header += it->second;
88+
hlist = curl_slist_append(hlist, header.c_str());
89+
}
90+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
6791
/** perform the actual query */
6892
res = curl_easy_perform(curl);
6993
if (res != CURLE_OK)
@@ -76,6 +100,7 @@ RestClient::response RestClient::get(const std::string& url)
76100
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
77101
ret.code = static_cast<int>(http_code);
78102

103+
curl_slist_free_all(hlist);
79104
curl_easy_cleanup(curl);
80105
curl_global_cleanup();
81106
}
@@ -94,11 +119,31 @@ RestClient::response RestClient::get(const std::string& url)
94119
RestClient::response RestClient::post(const std::string& url,
95120
const std::string& ctype,
96121
const std::string& data)
122+
{
123+
headermap emptyMap;
124+
return RestClient::post(url, ctype, data, emptyMap);
125+
}
126+
127+
/**
128+
* @brief HTTP POST method
129+
*
130+
* @param url to query
131+
* @param ctype content type as string
132+
* @param data HTTP POST body
133+
* @param headers HTTP headers
134+
*
135+
* @return response struct
136+
*/
137+
RestClient::response RestClient::post(const std::string& url,
138+
const std::string& ctype,
139+
const std::string& data,
140+
const headermap& headers)
97141
{
98142
/** create return struct */
99143
RestClient::response ret = {};
100144
/** build content-type header string */
101145
std::string ctype_header = "Content-Type: " + ctype;
146+
std::string header;
102147

103148
// use libcurl
104149
CURL *curl = NULL;
@@ -130,9 +175,15 @@ RestClient::response RestClient::post(const std::string& url,
130175
/** callback object for headers */
131176
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
132177
/** set content-type header */
133-
curl_slist* header = NULL;
134-
header = curl_slist_append(header, ctype_header.c_str());
135-
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
178+
curl_slist* hlist = NULL;
179+
hlist = curl_slist_append(hlist, ctype_header.c_str());
180+
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
181+
header = it->first;
182+
header += ':';
183+
header += it->second;
184+
hlist = curl_slist_append(hlist, header.c_str());
185+
}
186+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
136187
/** perform the actual query */
137188
res = curl_easy_perform(curl);
138189
if (res != CURLE_OK)
@@ -145,7 +196,7 @@ RestClient::response RestClient::post(const std::string& url,
145196
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
146197
ret.code = static_cast<int>(http_code);
147198

148-
curl_slist_free_all(header);
199+
curl_slist_free_all(hlist);
149200
curl_easy_cleanup(curl);
150201
curl_global_cleanup();
151202
}
@@ -164,11 +215,31 @@ RestClient::response RestClient::post(const std::string& url,
164215
RestClient::response RestClient::put(const std::string& url,
165216
const std::string& ctype,
166217
const std::string& data)
218+
{
219+
headermap emptyMap;
220+
return RestClient::put(url, ctype, data, emptyMap);
221+
}
222+
223+
/**
224+
* @brief HTTP PUT method
225+
*
226+
* @param url to query
227+
* @param ctype content type as string
228+
* @param data HTTP PUT body
229+
* @param headers HTTP headers
230+
*
231+
* @return response struct
232+
*/
233+
RestClient::response RestClient::put(const std::string& url,
234+
const std::string& ctype,
235+
const std::string& data,
236+
const headermap& headers)
167237
{
168238
/** create return struct */
169239
RestClient::response ret = {};
170240
/** build content-type header string */
171241
std::string ctype_header = "Content-Type: " + ctype;
242+
std::string header;
172243

173244
/** initialize upload object */
174245
RestClient::upload_object up_obj;
@@ -211,9 +282,15 @@ RestClient::response RestClient::put(const std::string& url,
211282
static_cast<long>(up_obj.length));
212283

213284
/** set content-type header */
214-
curl_slist* header = NULL;
215-
header = curl_slist_append(header, ctype_header.c_str());
216-
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
285+
curl_slist* hlist = NULL;
286+
hlist = curl_slist_append(hlist, ctype_header.c_str());
287+
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
288+
header = it->first;
289+
header += ':';
290+
header += it->second;
291+
hlist = curl_slist_append(hlist, header.c_str());
292+
}
293+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
217294
/** perform the actual query */
218295
res = curl_easy_perform(curl);
219296
if (res != CURLE_OK)
@@ -226,7 +303,7 @@ RestClient::response RestClient::put(const std::string& url,
226303
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
227304
ret.code = static_cast<int>(http_code);
228305

229-
curl_slist_free_all(header);
306+
curl_slist_free_all(hlist);
230307
curl_easy_cleanup(curl);
231308
curl_global_cleanup();
232309
}
@@ -241,6 +318,20 @@ RestClient::response RestClient::put(const std::string& url,
241318
* @return response struct
242319
*/
243320
RestClient::response RestClient::del(const std::string& url)
321+
{
322+
headermap emptyMap;
323+
return RestClient::del(url, emptyMap);
324+
}
325+
326+
/**
327+
* @brief HTTP DELETE method
328+
*
329+
* @param url to query
330+
* @param headers HTTP headers
331+
*
332+
* @return response struct
333+
*/
334+
RestClient::response RestClient::del(const std::string& url, const headermap& headers)
244335
{
245336
/** create return struct */
246337
RestClient::response ret = {};
@@ -251,6 +342,7 @@ RestClient::response RestClient::del(const std::string& url)
251342
// use libcurl
252343
CURL *curl = NULL;
253344
CURLcode res = CURLE_OK;
345+
std::string header;
254346

255347
curl = curl_easy_init();
256348
if (curl)
@@ -274,6 +366,15 @@ RestClient::response RestClient::del(const std::string& url)
274366
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
275367
/** callback object for headers */
276368
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ret);
369+
/** set http headers */
370+
curl_slist* hlist = NULL;
371+
for (std::map<std::string, std::string>::const_iterator it = headers.begin(); it != headers.end(); ++it) {
372+
header = it->first;
373+
header += ':';
374+
header += it->second;
375+
hlist = curl_slist_append(hlist, header.c_str());
376+
}
377+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
277378
/** perform the actual query */
278379
res = curl_easy_perform(curl);
279380
if (res != CURLE_OK)
@@ -286,6 +387,7 @@ RestClient::response RestClient::del(const std::string& url)
286387
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
287388
ret.code = static_cast<int>(http_code);
288389

390+
curl_slist_free_all(hlist);
289391
curl_easy_cleanup(curl);
290392
curl_global_cleanup();
291393
}
@@ -333,7 +435,7 @@ size_t RestClient::header_callback(void *data, size_t size, size_t nmemb,
333435
//roll with non seperated headers...
334436
trim(header);
335437
if ( 0 == header.length() ){
336-
return (size * nmemb); //blank line;
438+
return (size * nmemb); //blank line;
337439
}
338440
r->headers[header] = "present";
339441
} else {

0 commit comments

Comments
 (0)