Skip to content

Commit 8c0a365

Browse files
author
Levent KARAGÖL
committed
Documentation has been updated
1 parent 22d0ea8 commit 8c0a365

File tree

1 file changed

+273
-4
lines changed

1 file changed

+273
-4
lines changed

README.md

Lines changed: 273 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# libcpp-http-client
2+
23
Modern, non-blocking and exception free, header-only HTTP Client library for C++ (17+)
34

45
[![linux](https://github.com/lk-libs/libcpp-http-client/actions/workflows/linux.yml/badge.svg)](https://github.com/lk-libs/libcpp-http-client/actions/workflows/linux.yml)
56
[![windows](https://github.com/lk-libs/libcpp-http-client/actions/workflows/windows.yml/badge.svg)](https://github.com/lk-libs/libcpp-http-client/actions/workflows/windows.yml)
67

78

89
> [!NOTE]
9-
> 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+
1012

1113
# Table of Contents
14+
1215
* [How to add it to my project](#how-to-add-it-to-my-project)
1316
* [How to use? (Simplest way)](#how-to-use-simplest-way)
1417
* [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++
1821
* [POST request with form data](#post-request-with-form-data)
1922
* [POST request with JSON data](#post-request-with-json-data)
2023
* [What about others? (PUT, DELETE, PATCH)](#what-about-others-put-delete-patch)
24+
* [Full function list](#full-function-list)
25+
* [Semantic Versioning](#semantic-versioning)
2126
* [License](#license)
2227
* [Contact](#contact)
2328

@@ -30,7 +35,6 @@ But this library is a kind of Curl wrapper that uses Curl under the hood. So, yo
3035

3136
You can find usage examples in the examples folder. You can also find a sample CMakeLists.txt file content below.
3237

33-
3438
```cmake
3539
cmake_minimum_required(VERSION 3.14)
3640
@@ -52,7 +56,6 @@ Below you can see the simplest use case sending QueryString parameters to an API
5256
> [!TIP]
5357
> 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.
5458
55-
5659
```cpp
5760
#include <fstream>
5861
#include "libcpp-http-client.hpp"
@@ -72,6 +75,7 @@ int main() {
7275
}
7376
```
7477

78+
7579
## What does exception free mean?
7680

7781
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() {
9195
// Instead of throwing an exception, the succeed field of the response object is set to false
9296
std::cout << "Succeed: " << response.succeed << std::endl;
9397

94-
// 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)
9599
std::cout << "Http Status Code: " << response.statusCode << std::endl;
96100

97101
// Also if any error message is available, it is set to the errorMessage field
@@ -101,18 +105,283 @@ int main() {
101105
}
102106
```
103107

108+
104109
## What does non-blocking mean?
105110

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+
using namespace lklibs;
118+
119+
int main() {
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+
using namespace lklibs;
140+
141+
int main() {
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+
106164
## What about binary data?
107165

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&lt;unsigned char&gt;"*** 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+
using namespace lklibs;
173+
174+
int main() {
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();
178+
179+
std::cout << "Succeed: " << response.succeed << std::endl;
180+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
181+
182+
// In this case, you can get the data via binaryData instead of textData
183+
std::cout << "Data Size: " << response.binaryData.size() << std::endl;
184+
185+
return 0;
186+
}
187+
```
188+
189+
108190
## Sending Custom HTTP Headers
109191

192+
If you need to send custom HTTP HEADERs during the request, you can send them in std::map<std::string, std::string> format.
193+
194+
```cpp
195+
#include <fstream>
196+
#include "libcpp-http-client.hpp"
197+
198+
using namespace lklibs;
199+
200+
int main() {
201+
202+
// You can send custom headers in a string/string map
203+
auto headers = std::map<std::string, std::string>();
204+
205+
headers["Custom-Header1"] = "value1";
206+
headers["Custom-Header2"] = "value2";
207+
208+
auto response = HttpClient::getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();
209+
210+
std::cout << "Succeed: " << response.succeed << std::endl;
211+
212+
return 0;
213+
}
214+
```
215+
216+
110217
## POST request with form data
111218

219+
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+
using namespace lklibs;
226+
227+
int main() {
228+
229+
// You can send a POST request with form data in the payload
230+
std::string payload = "param1=7&param2=test";
231+
232+
auto response = HttpClient::postRequest("https://api.myproject.com", payload).get();
233+
234+
std::cout << "Succeed: " << response.succeed << std::endl;
235+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
236+
std::cout << "Data: " << response.textData << std::endl;
237+
238+
return 0;
239+
}
240+
```
241+
242+
112243
## POST request with JSON data
113244

245+
Sending JSON data via HTTP POST is not much different. Just remember to send **"Content-Type"** as **"application/json"** via HTTP HEADER.
246+
247+
```cpp
248+
#include <fstream>
249+
#include "libcpp-http-client.hpp"
250+
251+
using namespace lklibs;
252+
253+
int main() {
254+
255+
std::string payload = R"({"param1": 7, "param2": "test"})";
256+
257+
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
258+
auto headers = std::map<std::string, std::string>();
259+
260+
headers["Content-Type"] = "application/json";
261+
262+
auto response = HttpClient::postRequest("https://api.myproject.com", payload, headers).get();
263+
264+
std::cout << "Succeed: " << response.succeed << std::endl;
265+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
266+
std::cout << "Data: " << response.textData << std::endl;
267+
268+
return 0;
269+
}
270+
```
271+
272+
114273
## What about others? (PUT, DELETE, PATCH)
115274

275+
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+
using namespace lklibs;
282+
283+
int main() {
284+
285+
std::string payload = "param1=7&param2=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&param2=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.
314+
315+
* getRequest
316+
* std::future<HttpResult> getRequest(const std::string &url)
317+
* std::future<HttpResult> getRequest(const std::string &url, bool returnAsBinary)
318+
* std::future<HttpResult> getRequest(const std::string &url, const std::map<std::string, std::string> &headers)
319+
* std::future<HttpResult> getRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
320+
321+
322+
* postRequest
323+
* std::future<HttpResult> postRequest(const std::string &url)
324+
* std::future<HttpResult> postRequest(const std::string &url, const std::string &payload)
325+
* std::future<HttpResult> postRequest(const std::string &url, bool returnAsBinary)
326+
* std::future<HttpResult> postRequest(const std::string &url, const std::map<std::string, std::string> &headers)
327+
* std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
328+
* std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
329+
* std::future<HttpResult> postRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
330+
* std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
331+
332+
333+
* putRequest
334+
* std::future<HttpResult> putRequest(const std::string &url)
335+
* std::future<HttpResult> putRequest(const std::string &url, const std::string &payload)
336+
* std::future<HttpResult> putRequest(const std::string &url, bool returnAsBinary)
337+
* std::future<HttpResult> putRequest(const std::string &url, const std::map<std::string, std::string> &headers)
338+
* std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
339+
* std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
340+
* std::future<HttpResult> putRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
341+
* std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
342+
343+
344+
* deleteRequest
345+
* std::future<HttpResult> deleteRequest(const std::string &url)
346+
* std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload)
347+
* std::future<HttpResult> deleteRequest(const std::string &url, bool returnAsBinary)
348+
* std::future<HttpResult> deleteRequest(const std::string &url, const std::map<std::string, std::string> &headers)
349+
* std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
350+
* std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
351+
* std::future<HttpResult> deleteRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
352+
* std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
353+
354+
355+
* patchRequest
356+
* std::future<HttpResult> patchRequest(const std::string &url)
357+
* std::future<HttpResult> patchRequest(const std::string &url, bool returnAsBinary)
358+
* std::future<HttpResult> patchRequest(const std::string &url, const std::map<std::string, std::string> &headers)
359+
* std::future<HttpResult> patchRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
360+
361+
116362
## License
117363

364+
MIT License
365+
366+
Copyright (c) 2024 lk-libs, Levent KARAGÖL
367+
368+
Permission is hereby granted, free of charge, to any person obtaining a copy
369+
of this software and associated documentation files (the "Software"), to deal
370+
in the Software without restriction, including without limitation the rights
371+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
372+
copies of the Software, and to permit persons to whom the Software is
373+
furnished to do so, subject to the following conditions:
374+
375+
The above copyright notice and this permission notice shall be included in all
376+
copies or substantial portions of the Software.
377+
378+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
379+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
380+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
381+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
382+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
383+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
384+
SOFTWARE.
385+
386+
118387
## Contact

0 commit comments

Comments
 (0)