@@ -59,7 +59,7 @@ target_link_libraries(myProject PRIVATE libcpp-http-client CURL::libcurl)
59
59
Below you can see the simplest use case sending QueryString parameters to an API via HTTP GET.
60
60
61
61
> [ !IMPORTANT]
62
- > Please do not use it this way, if more than one call will be made . You do not use the non-blocking
62
+ > Please do not use it this way, if more than one call will be made. You do not use the non-blocking
63
63
> feature in this way. Just keep reading...
64
64
65
65
``` cpp
@@ -70,10 +70,13 @@ using namespace lklibs;
70
70
71
71
int main () {
72
72
73
- HttpClient httpClient ;
73
+ HttpRequest httpRequest("https://api.myproject.com") ;
74
74
75
75
// The simplest but slowest method if multiple calls will be made
76
- auto response = httpClient.getRequest("https://api.myproject.com?param1=7¶m2=test").get();
76
+ auto response = httpRequest
77
+ .setQueryString("param1=7¶m2=test")
78
+ .send()
79
+ .get();
77
80
78
81
std::cout << "Succeed: " << response.succeed << std::endl;
79
82
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -98,13 +101,17 @@ using namespace lklibs;
98
101
99
102
int main () {
100
103
101
- HttpClient httpClient;
102
-
103
- auto response1 = httpClient.getRequest("https://api.myproject.com/foo").get();
104
- auto response2 = httpClient.getRequest("https://api.myproject.com/bar").get();
105
- auto response3 = httpClient.getRequest("https://api.myproject.com/baz").get();
106
- auto response4 = httpClient.getRequest("https://api.myproject.com/qux").get();
107
- auto response5 = httpClient.getRequest("https://api.myproject.com/quux").get();
104
+ HttpRequest httpRequest1("https://api.myproject.com/foo");
105
+ HttpRequest httpRequest2("https://api.myproject.com/bar");
106
+ HttpRequest httpRequest3("https://api.myproject.com/baz");
107
+ HttpRequest httpRequest4("https://api.myproject.com/qux");
108
+ HttpRequest httpRequest5("https://api.myproject.com/quux");
109
+
110
+ auto response1 = httpRequest1.send().get();
111
+ auto response2 = httpRequest2.send().get();
112
+ auto response3 = httpRequest3.send().get();
113
+ auto response4 = httpRequest4.send().get();
114
+ auto response5 = httpRequest5.send().get();
108
115
109
116
// Takes 2.5 seconds in total
110
117
@@ -124,13 +131,17 @@ using namespace lklibs;
124
131
125
132
int main () {
126
133
127
- HttpClient httpClient;
128
-
129
- auto future1 = httpClient.getRequest("https://api.myproject.com/foo");
130
- auto future2 = httpClient.getRequest("https://api.myproject.com/bar");
131
- auto future3 = httpClient.getRequest("https://api.myproject.com/baz");
132
- auto future4 = httpClient.getRequest("https://api.myproject.com/qux");
133
- auto future5 = httpClient.getRequest("https://api.myproject.com/quux");
134
+ HttpRequest httpRequest1("https://api.myproject.com/foo");
135
+ HttpRequest httpRequest2("https://api.myproject.com/bar");
136
+ HttpRequest httpRequest3("https://api.myproject.com/baz");
137
+ HttpRequest httpRequest4("https://api.myproject.com/qux");
138
+ HttpRequest httpRequest5("https://api.myproject.com/quux");
139
+
140
+ auto future1 = httpRequest.send();
141
+ auto future2 = httpRequest.send();
142
+ auto future3 = httpRequest.send();
143
+ auto future4 = httpRequest.send();
144
+ auto future5 = httpRequest.send();
134
145
135
146
auto response1 = future1.get();
136
147
auto response2 = future2.get();
@@ -144,7 +155,7 @@ int main() {
144
155
}
145
156
```
146
157
147
- All functions in the library return a future and allow the next line to run without blocking the flow.
158
+ ** "send" ** function in the library return a future and allow the next line to run without blocking the flow.
148
159
149
160
150
161
## What does exception free mean?
@@ -166,9 +177,9 @@ using namespace lklibs;
166
177
167
178
int main () {
168
179
169
- HttpClient httpClient ;
180
+ HttpRequest httpRequest("https://www.myinvalidurl.com") ;
170
181
171
- auto response = httpClient.getRequest("https://www.myinvalidurl.com" ).get();
182
+ auto response = httpRequest.send( ).get();
172
183
173
184
// Instead of throwing an exception, the succeed field of the response object is set to false
174
185
std::cout << "Succeed: " << response.succeed << std::endl;
@@ -189,8 +200,7 @@ int main() {
189
200
In the examples so far, we have used the ** "textData"** property of the returning response object.
190
201
However, we need binary data for requests made to binary files such as images. In such cases,
191
202
we can ensure that the returned data is returned in ** "binaryData"** of type
192
- *** "std::vector< ; unsigned char> ; "*** instead of ** "textData"** by passing the value ** "true"**
193
- to the ** "returnAsBinary"** parameter.
203
+ *** "std::vector< ; unsigned char> ; "*** instead of ** "textData"** by calling ** "returnAsBinary()"** method before send as follow.
194
204
195
205
``` cpp
196
206
#include < fstream>
@@ -200,10 +210,13 @@ using namespace lklibs;
200
210
201
211
int main () {
202
212
203
- HttpClient httpClient ;
213
+ HttpRequest httpRequest("https://api.myproject.com/image/7") ;
204
214
205
- // If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
206
- auto response = httpClient.getRequest("https://api.myproject.com/image/7", true).get();
215
+ // If you need to retrieve binary data such as an image, just call the "returnAsBinary" method before send
216
+ auto response = httpRequest
217
+ .returnAsBinary()
218
+ .send()
219
+ .get();
207
220
208
221
std::cout << "Succeed: " << response.succeed << std::endl;
209
222
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -218,8 +231,7 @@ int main() {
218
231
219
232
## Sending custom HTTP headers
220
233
221
- If you need to send custom HTTP HEADERs during the request, you can send them in
222
- std::map<std::string, std::string> format.
234
+ If you need to send custom HTTP HEADERs during the request, you can add them to the request as key-value pairs with ** "addHeader()"** method.
223
235
224
236
``` cpp
225
237
#include < fstream>
@@ -229,16 +241,15 @@ using namespace lklibs;
229
241
230
242
int main () {
231
243
232
- HttpClient httpClient;
233
-
234
- // You can send custom headers in a string/string map
235
- auto headers = std::map<std::string, std::string>();
236
-
237
- headers["Custom-Header1"] = "value1";
238
- headers["Custom-Header2"] = "value2";
239
-
240
- auto response = httpClient.getRequest("https://api.myproject.com?param1=7¶m2=test", headers).get();
241
-
244
+ HttpRequest httpRequest("https://api.myproject.com");
245
+
246
+ // You can send custom headers as key-value pairs
247
+ auto response = httpRequest
248
+ .addHeader("Custom-Header1", "value1")
249
+ .addHeader("Custom-Header2", "value2")
250
+ .send()
251
+ .get();
252
+
242
253
std::cout << "Succeed: " << response.succeed << std::endl;
243
254
244
255
return 0;
@@ -248,8 +259,7 @@ int main() {
248
259
249
260
## POST request with form data
250
261
251
- Next is submitting form data via HTTP POST. All you have to do is use ** "postRequest"** instead
252
- of ** "getRequest"** . You can pass the form data to the ** "payload"** variable as seen in the sample code below.
262
+ Next is submitting form data via HTTP POST. All you have to do is use ** "setMethod"** to change HTTP method type. You can pass the form data with ** "setPaylod"** method as seen in the sample code below.
253
263
254
264
``` cpp
255
265
#include < fstream>
@@ -259,12 +269,14 @@ using namespace lklibs;
259
269
260
270
int main () {
261
271
262
- HttpClient httpClient ;
272
+ HttpRequest httpRequest("https://api.myproject.com") ;
263
273
264
274
// You can send a POST request with form data in the payload
265
- std::string payload = "param1=7¶m2=test";
266
-
267
- auto response = httpClient.postRequest("https://api.myproject.com", payload).get();
275
+ auto response = httpRequest
276
+ .setMethod(HttpMethod::POST)
277
+ .setPayload("param1=7¶m2=test")
278
+ .send()
279
+ .get();
268
280
269
281
std::cout << "Succeed: " << response.succeed << std::endl;
270
282
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -288,16 +300,15 @@ using namespace lklibs;
288
300
289
301
int main () {
290
302
291
- HttpClient httpClient;
292
-
293
- std::string payload = R"({"param1": 7, "param2": "test"})";
294
-
303
+ HttpRequest httpRequest("https://api.myproject.com");
304
+
295
305
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
296
- auto headers = std::map<std::string, std::string>();
297
-
298
- headers["Content-Type"] = "application/json";
299
-
300
- auto response = httpClient.postRequest("https://api.myproject.com", payload, headers).get();
306
+ auto response = httpRequest
307
+ .setMethod(HttpMethod::POST)
308
+ .setPayload(R"({"param1": 7, "param2": "test"})")
309
+ .addHeader("Content-Type", "application/json")
310
+ .send()
311
+ .get();
301
312
302
313
std::cout << "Succeed: " << response.succeed << std::endl;
303
314
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -320,13 +331,26 @@ using namespace lklibs;
320
331
321
332
int main () {
322
333
323
- HttpClient httpClient ;
334
+ HttpRequest httpRequest1("https://api.myproject.com") ;
324
335
325
- std::string payload = "param1=7¶m2=test";
336
+ auto future1 = httpRequest
337
+ .setMethod(HttpMethod::PUT)
338
+ .setPayload("param1=7¶m2=test")
339
+ .send();
340
+
341
+ HttpRequest httpRequest2("https://api.myproject.com");
326
342
327
- auto future1 = httpClient.putRequest("https://api.myproject.com", payload);
328
- auto future2 = httpClient.deleteRequest("https://api.myproject.com", payload);
329
- auto future3 = httpClient.patchRequest("https://api.myproject.com?param1=7¶m2=test");
343
+ auto future2 = httpRequest
344
+ .setMethod(HttpMethod::DELETE_)
345
+ .setPayload("param1=7¶m2=test")
346
+ .send();
347
+
348
+ HttpRequest httpRequest3("https://api.myproject.com");
349
+
350
+ auto future3 = httpRequest
351
+ .setMethod(HttpMethod::PATCH)
352
+ .setQueryString("param1=7¶m2=test")
353
+ .send();
330
354
331
355
auto response1 = future1.get();
332
356
auto response2 = future2.get();
@@ -339,9 +363,8 @@ int main() {
339
363
340
364
## How to ignore SSL certificate errors?
341
365
342
- If you need to ignore SSL certificate errors for any valid reason, you can continue
343
- working by passing ** "true"** value to the ** "ignoreSslErrors"** variable of the
344
- HttpClient class.
366
+ If you need to ignore SSL certificate errors for any valid reason, you can call "ignoreSslErrors"
367
+ method before sending the request.
345
368
346
369
``` cpp
347
370
#include < fstream>
@@ -351,12 +374,13 @@ using namespace lklibs;
351
374
352
375
int main () {
353
376
354
- HttpClient httpClient;
355
-
356
- // If you need to ignore SSL errors, you can set the "ignoreSslErrors" field to true
357
- httpClient.ignoreSslErrors = true;
377
+ HttpRequest httpRequest("https://api.myinvalidssl.com");
358
378
359
- auto response = httpClient.getRequest("https://api.myinvalidssl.com").get();
379
+ // If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
380
+ auto response = httpRequest
381
+ .ignoreSslErrors()
382
+ .send()
383
+ .get();
360
384
361
385
return 0;
362
386
}
@@ -381,58 +405,21 @@ section to the documentation.
381
405
382
406
## Full function list
383
407
384
- You can find the complete list of functions in the library below. In fact, they are just
385
- overloaded versions of 5 functions in total .
408
+ You can find the complete list of functions in the library below. Since all methods except
409
+ send return the class itself, so they can be added one after the other like a chain .
386
410
387
411
> [ !TIP]
388
412
> All methods and parameters descriptions are also available within the code as comment for IDEs.
389
413
390
414
``` cpp
391
- - getRequest
392
- - std::future<HttpResult> getRequest (const std::string &url)
393
- - std::future<HttpResult > getRequest(const std::string &url, bool returnAsBinary)
394
- - std::future<HttpResult > getRequest(const std::string &url, const std::map<std::string, std::string> &headers)
395
- - std::future<HttpResult > getRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
396
-
397
-
398
- - postRequest
399
- - std::future<HttpResult > postRequest(const std::string &url)
400
- - std::future<HttpResult > postRequest(const std::string &url, const std::string &payload)
401
- - std::future<HttpResult > postRequest(const std::string &url, bool returnAsBinary)
402
- - std::future<HttpResult > postRequest(const std::string &url, const std::map<std::string, std::string> &headers)
403
- - std::future<HttpResult > postRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
404
- - std::future<HttpResult > postRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
405
- - std::future<HttpResult > postRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
406
- - std::future<HttpResult > postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
407
-
408
-
409
- - putRequest
410
- - std::future<HttpResult > putRequest(const std::string &url)
411
- - std::future<HttpResult > putRequest(const std::string &url, const std::string &payload)
412
- - std::future<HttpResult > putRequest(const std::string &url, bool returnAsBinary)
413
- - std::future<HttpResult > putRequest(const std::string &url, const std::map<std::string, std::string> &headers)
414
- - std::future<HttpResult > putRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
415
- - std::future<HttpResult > putRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
416
- - std::future<HttpResult > putRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
417
- - std::future<HttpResult > putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
418
-
419
-
420
- - deleteRequest
421
- - std::future<HttpResult > deleteRequest(const std::string &url)
422
- - std::future<HttpResult > deleteRequest(const std::string &url, const std::string &payload)
423
- - std::future<HttpResult > deleteRequest(const std::string &url, bool returnAsBinary)
424
- - std::future<HttpResult > deleteRequest(const std::string &url, const std::map<std::string, std::string> &headers)
425
- - std::future<HttpResult > deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
426
- - std::future<HttpResult > deleteRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
427
- - std::future<HttpResult > deleteRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
428
- - std::future<HttpResult > deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
429
-
430
-
431
- - patchRequest
432
- - std::future<HttpResult > patchRequest(const std::string &url)
433
- - std::future<HttpResult > patchRequest(const std::string &url, bool returnAsBinary)
434
- - std::future<HttpResult > patchRequest(const std::string &url, const std::map<std::string, std::string> &headers)
435
- - std::future<HttpResult > patchRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
415
+ - HttpRequest &setMethod (const HttpMethod &method) noexcept
416
+ - HttpRequest &setQueryString(const std::string &queryString) noexcept
417
+ - HttpRequest &setPayload(const std::string &payload) noexcept
418
+ - HttpRequest &returnAsBinary() noexcept
419
+ - HttpRequest &ignoreSslErrors() noexcept
420
+ - HttpRequest &addHeader(const std::string &key, const std::string &value) noexcept
421
+ - std::future<HttpResult > send() noexcept
422
+
436
423
```
437
424
438
425
0 commit comments