You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Please read this document before using the library. I know you don't have time, but reading this document will save you time. What I call a document is just this file. Trial and error will cost you more time.
10
+
> Please read this document before using the library. I know you don't have time, but reading this document will save you time. I mean just this file, it's not long at all. Trial and error will cost you more time.
11
+
10
12
11
13
# Table of Contents
14
+
12
15
*[How to add it to my project](#how-to-add-it-to-my-project)
13
16
*[How to use? (Simplest way)](#how-to-use-simplest-way)
14
17
*[What does exception free mean?](#what-does-exception-free-mean)
@@ -18,6 +21,8 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++
18
21
*[POST request with form data](#post-request-with-form-data)
19
22
*[POST request with JSON data](#post-request-with-json-data)
20
23
*[What about others? (PUT, DELETE, PATCH)](#what-about-others-put-delete-patch)
24
+
*[Full function list](#full-function-list)
25
+
*[Semantic Versioning](#semantic-versioning)
21
26
*[License](#license)
22
27
*[Contact](#contact)
23
28
@@ -30,7 +35,6 @@ But this library is a kind of Curl wrapper that uses Curl under the hood. So, yo
30
35
31
36
You can find usage examples in the examples folder. You can also find a sample CMakeLists.txt file content below.
32
37
33
-
34
38
```cmake
35
39
cmake_minimum_required(VERSION 3.14)
36
40
@@ -52,7 +56,6 @@ Below you can see the simplest use case sending QueryString parameters to an API
52
56
> [!TIP]
53
57
> Please do not use it this way, if more than one call will be made . You do not use the non-blocking feature in this way.
54
58
55
-
56
59
```cpp
57
60
#include<fstream>
58
61
#include"libcpp-http-client.hpp"
@@ -72,6 +75,7 @@ int main() {
72
75
}
73
76
```
74
77
78
+
75
79
## What does exception free mean?
76
80
77
81
Exception Free means that no exception will be thrown for any call you make to this library. If the URL cannot be found, there is a timeout, there is an authorization problem or another error occurs on the server, the bool typed "succeed" field of the response is returned as false. In addition, the HTTP Status Code value returned from the server is returned in the int typed "statusCode" field and possibly additional error information that may be returned from the server is returned in the string typed "errorMessage" field.
@@ -91,7 +95,7 @@ int main() {
91
95
// Instead of throwing an exception, the succeed field of the response object is set to false
// And the http status code is set to the statusCode field
98
+
// And the http status code is set to the statusCode field (404 in this case)
95
99
std::cout << "Http Status Code: " << response.statusCode << std::endl;
96
100
97
101
// Also if any error message is available, it is set to the errorMessage field
@@ -101,18 +105,283 @@ int main() {
101
105
}
102
106
```
103
107
108
+
104
109
## What does non-blocking mean?
105
110
111
+
Let's talk about this through an example. Let's say we call 5 different API methods when our screen opens and each one takes an average of 500 ms. If we call these methods one after another as follows, we will get all the answers in 2.5 seconds in total.
112
+
113
+
```cpp
114
+
#include<fstream>
115
+
#include"libcpp-http-client.hpp"
116
+
117
+
usingnamespacelklibs;
118
+
119
+
intmain() {
120
+
121
+
auto response1 = HttpClient::getRequest("https://api.myproject.com/foo").get();
122
+
auto response2 = HttpClient::getRequest("https://api.myproject.com/bar").get();
123
+
auto response3 = HttpClient::getRequest("https://api.myproject.com/baz").get();
124
+
auto response4 = HttpClient::getRequest("https://api.myproject.com/qux").get();
125
+
auto response5 = HttpClient::getRequest("https://api.myproject.com/quux").get();
126
+
127
+
// Take 2.5 seconds in total
128
+
129
+
return 0;
130
+
}
131
+
```
132
+
133
+
".get()" call at the end of each line causes us to wait until the server responds. However, if we make the same call as follows, all requests will be processed in parallel and the total time will take approximately 0.5 seconds.
134
+
135
+
```cpp
136
+
#include<fstream>
137
+
#include"libcpp-http-client.hpp"
138
+
139
+
usingnamespacelklibs;
140
+
141
+
intmain() {
142
+
143
+
auto future1 = HttpClient::getRequest("https://api.myproject.com/foo");
144
+
auto future2 = HttpClient::getRequest("https://api.myproject.com/bar");
145
+
auto future3 = HttpClient::getRequest("https://api.myproject.com/baz");
146
+
auto future4 = HttpClient::getRequest("https://api.myproject.com/qux");
147
+
auto future5 = HttpClient::getRequest("https://api.myproject.com/quux");
148
+
149
+
auto response1 = future1.get();
150
+
auto response2 = future2.get();
151
+
auto response3 = future3.get();
152
+
auto response4 = future4.get();
153
+
auto response5 = future5.get();
154
+
155
+
// Take 0.5 seconds in total
156
+
157
+
return 0;
158
+
}
159
+
```
160
+
161
+
All functions in the library return a future and allow the next line to run without blocking the flow.
162
+
163
+
106
164
## What about binary data?
107
165
166
+
In the examples so far, we have used the **"textData"** property of the returning response object. However, we need binary data for requests made to binary files such as images. In such cases, we can ensure that the returned data is returned in **"binaryData"** of type ***"std::vector<unsigned char>"*** instead of **"textData"** by passing the value **"true"** to the **"returnAsBinary"** parameter.
167
+
168
+
```cpp
169
+
#include<fstream>
170
+
#include"libcpp-http-client.hpp"
171
+
172
+
usingnamespacelklibs;
173
+
174
+
intmain() {
175
+
176
+
// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
177
+
auto response = HttpClient::getRequest("https://api.myproject.com/image/7", true).get();
Next is submitting form data via HTTP POST. All you have to do is use **"postRequest"** instead of **"getRequest"**. You can pass the form data to the **"payload"** variable as seen in the sample code below.
220
+
221
+
```cpp
222
+
#include<fstream>
223
+
#include"libcpp-http-client.hpp"
224
+
225
+
usingnamespacelklibs;
226
+
227
+
intmain() {
228
+
229
+
// You can send a POST request with form data in the payload
230
+
std::string payload = "param1=7¶m2=test";
231
+
232
+
auto response = HttpClient::postRequest("https://api.myproject.com", payload).get();
You can also find the usage of other methods in the sample code below.
276
+
277
+
```cpp
278
+
#include<fstream>
279
+
#include"libcpp-http-client.hpp"
280
+
281
+
usingnamespacelklibs;
282
+
283
+
intmain() {
284
+
285
+
std::string payload = "param1=7¶m2=test";
286
+
287
+
auto future1 = HttpClient::putRequest("https://api.myproject.com", payload);
288
+
auto future2 = HttpClient::deleteRequest("https://api.myproject.com", payload);
289
+
auto future3 = HttpClient::patchRequest("https://api.myproject.com?param1=7¶m2=test");
290
+
291
+
auto response1 = future1.get();
292
+
auto response2 = future2.get();
293
+
auto response3 = future3.get();
294
+
295
+
return 0;
296
+
}
297
+
```
298
+
299
+
300
+
## Semantic Versioning
301
+
302
+
Versioning of the library is done using conventional semantic versioning. Accordingly, in the versioning made as **MAJOR.MINOR.PATCH**;
303
+
304
+
**PATCH:** Includes possible Bug&Fixes and improvements. You definitely want to get this.
305
+
306
+
**MINOR:** Additional functionality added via backwards compatibility. You probably want to get this, it doesn't hurt.
307
+
308
+
**MAJOR:** Additional functionality that breaks backwards compatibility. You'll need to know what's changed before you get it, and you'll probably have to make changes to your own code. If I publish something like this, I will definitely add the changes required for migration section to the documentation.
309
+
310
+
311
+
## Full function list
312
+
313
+
You can find the complete list of functions in the library below. In fact, they are just overloaded versions of 5 functions in total.
0 commit comments