@@ -5,8 +5,10 @@ using namespace lklibs;
55
66void simpleGet () {
77
8+ HttpClient httpClient;
9+
810 // The simplest but slowest method if multiple calls will be made
9- auto response = HttpClient:: getRequest (" https://httpbun.com/get?param1=7¶m2=test" ).get ();
11+ auto response = httpClient. getRequest (" https://httpbun.com/get?param1=7¶m2=test" ).get ();
1012
1113 std::cout << " Succeed: " << response.succeed << std::endl;
1214 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -15,10 +17,12 @@ void simpleGet() {
1517
1618void nonBlockingGet () {
1719
20+ HttpClient httpClient;
21+
1822 // All requests are made one after the other without waiting for a response
19- auto future1 = HttpClient:: getRequest (" https://httpbun.com/get?param1=1¶m2=test1" );
20- auto future2 = HttpClient:: getRequest (" https://httpbun.com/get?param1=2¶m2=test2" );
21- auto future3 = HttpClient:: getRequest (" https://httpbun.com/get?param1=3¶m2=test3" );
23+ auto future1 = httpClient. getRequest (" https://httpbun.com/get?param1=1¶m2=test1" );
24+ auto future2 = httpClient. getRequest (" https://httpbun.com/get?param1=2¶m2=test2" );
25+ auto future3 = httpClient. getRequest (" https://httpbun.com/get?param1=3¶m2=test3" );
2226
2327 // Then all the answers are received. Thus, 3 requests are sent in parallel
2428 auto response1 = future1.get ();
@@ -40,8 +44,10 @@ void nonBlockingGet() {
4044
4145void receiveBinaryData () {
4246
47+ HttpClient httpClient;
48+
4349 // If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
44- auto response = HttpClient:: getRequest (" https://httpbun.com/bytes/100" , true ).get ();
50+ auto response = httpClient. getRequest (" https://httpbun.com/bytes/100" , true ).get ();
4551
4652 std::cout << " Succeed: " << response.succeed << std::endl;
4753 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -52,8 +58,10 @@ void receiveBinaryData() {
5258
5359void receiveError () {
5460
61+ HttpClient httpClient;
62+
5563 // This is an exception free library. If an error occurs, no exception is thrown
56- auto response = HttpClient:: getRequest (" https://httpbun.com/not_found" ).get ();
64+ auto response = httpClient. getRequest (" https://httpbun.com/not_found" ).get ();
5765
5866 // Instead, the succeed field of the response object is set to false
5967 std::cout << " Succeed: " << response.succeed << std::endl;
@@ -67,23 +75,27 @@ void receiveError() {
6775
6876void sendingHttpHeaders () {
6977
78+ HttpClient httpClient;
79+
7080 // You can send custom headers in a string/string map
7181 auto headers = std::map<std::string, std::string>();
7282
7383 headers[" Custom-Header1" ] = " value1" ;
7484 headers[" Custom-Header2" ] = " value2" ;
7585
76- auto response = HttpClient:: getRequest (" https://httpbun.com/get?param1=7¶m2=test" , headers).get ();
86+ auto response = httpClient. getRequest (" https://httpbun.com/get?param1=7¶m2=test" , headers).get ();
7787
7888 std::cout << " Succeed: " << response.succeed << std::endl;
7989}
8090
8191void simplePostWithFormData () {
8292
93+ HttpClient httpClient;
94+
8395 // You can send a POST request with form data in the payload
8496 std::string payload = " param1=7¶m2=test" ;
8597
86- auto response = HttpClient:: postRequest (" https://httpbun.com/post" , payload).get ();
98+ auto response = httpClient. postRequest (" https://httpbun.com/post" , payload).get ();
8799
88100 std::cout << " Succeed: " << response.succeed << std::endl;
89101 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -92,14 +104,16 @@ void simplePostWithFormData() {
92104
93105void simplePostWithJSONData () {
94106
107+ HttpClient httpClient;
108+
95109 std::string payload = R"( {"param1": 7, "param2": "test"})" ;
96110
97111 // You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
98112 auto headers = std::map<std::string, std::string>();
99113
100114 headers[" Content-Type" ] = " application/json" ;
101115
102- auto response = HttpClient:: postRequest (" https://httpbun.com/post" , payload, headers).get ();
116+ auto response = httpClient. postRequest (" https://httpbun.com/post" , payload, headers).get ();
103117
104118 std::cout << " Succeed: " << response.succeed << std::endl;
105119 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -108,10 +122,12 @@ void simplePostWithJSONData() {
108122
109123void simplePutWithFormData () {
110124
125+ HttpClient httpClient;
126+
111127 // You can send a PUT request with form data in the payload just like POST
112128 std::string payload = " param1=7¶m2=test" ;
113129
114- auto response = HttpClient:: putRequest (" https://httpbun.com/put" , payload).get ();
130+ auto response = httpClient. putRequest (" https://httpbun.com/put" , payload).get ();
115131
116132 std::cout << " Succeed: " << response.succeed << std::endl;
117133 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -120,10 +136,12 @@ void simplePutWithFormData() {
120136
121137void simpleDeleteWithFormData () {
122138
139+ HttpClient httpClient;
140+
123141 // You can send a DELETE request with form data in the payload just like POST
124142 std::string payload = " param1=7¶m2=test" ;
125143
126- auto response = HttpClient:: deleteRequest (" https://httpbun.com/delete" , payload).get ();
144+ auto response = httpClient. deleteRequest (" https://httpbun.com/delete" , payload).get ();
127145
128146 std::cout << " Succeed: " << response.succeed << std::endl;
129147 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -132,8 +150,10 @@ void simpleDeleteWithFormData() {
132150
133151void simplePatch () {
134152
153+ HttpClient httpClient;
154+
135155 // You can send a PATCH request with QueryString just like GET
136- auto response = HttpClient:: patchRequest (" https://httpbun.com/patch?param1=7¶m2=test" ).get ();
156+ auto response = httpClient. patchRequest (" https://httpbun.com/patch?param1=7¶m2=test" ).get ();
137157
138158 std::cout << " Succeed: " << response.succeed << std::endl;
139159 std::cout << " Http Status Code: " << response.statusCode << std::endl;
0 commit comments