3
3
4
4
using namespace lklibs ;
5
5
6
- void simpleGet () {
7
-
6
+ void simpleGet ()
7
+ {
8
8
HttpRequest httpRequest (" https://httpbun.com/get" );
9
9
10
10
// The simplest but slowest method if multiple calls will be made
11
11
auto response = httpRequest
12
- .setQueryString (" param1=7¶m2=test" )
13
- .send ()
14
- .get ();
12
+ .setQueryString (" param1=7¶m2=test" )
13
+ .send ()
14
+ .get ();
15
15
16
16
std::cout << " Succeed: " << response.succeed << std::endl;
17
17
std::cout << " Http Status Code: " << response.statusCode << std::endl;
18
18
std::cout << " Data: " << response.textData << std::endl;
19
19
}
20
20
21
- void nonBlockingGet () {
22
-
21
+ void nonBlockingGet ()
22
+ {
23
23
HttpRequest httpRequest1 (" https://httpbun.com/get" );
24
24
HttpRequest httpRequest2 (" https://httpbun.com/get" );
25
25
HttpRequest httpRequest3 (" https://httpbun.com/get" );
@@ -47,15 +47,15 @@ void nonBlockingGet() {
47
47
std::cout << " Response3 Data: " << response3.textData << std::endl;
48
48
}
49
49
50
- void receiveBinaryData () {
51
-
50
+ void receiveBinaryData ()
51
+ {
52
52
HttpRequest httpRequest (" https://httpbun.com/bytes/100" );
53
53
54
54
// If you need to retrieve binary data such as an image, just call the "returnAsBinary" method before send
55
55
auto response = httpRequest
56
- .returnAsBinary ()
57
- .send ()
58
- .get ();
56
+ .returnAsBinary ()
57
+ .send ()
58
+ .get ();
59
59
60
60
std::cout << " Succeed: " << response.succeed << std::endl;
61
61
std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -64,8 +64,8 @@ void receiveBinaryData() {
64
64
std::cout << " Data Size: " << response.binaryData .size () << std::endl;
65
65
}
66
66
67
- void receiveError () {
68
-
67
+ void receiveError ()
68
+ {
69
69
HttpRequest httpRequest (" https://httpbun.com/not_found" );
70
70
71
71
// This is an exception free library. If an error occurs, no exception is thrown
@@ -81,103 +81,103 @@ void receiveError() {
81
81
std::cout << " Error Message: " << response.errorMessage << std::endl;
82
82
}
83
83
84
- void sendingHttpHeaders () {
85
-
84
+ void sendingHttpHeaders ()
85
+ {
86
86
HttpRequest httpRequest (" https://httpbun.com/get?param1=7¶m2=test" );
87
87
88
88
// You can send custom headers as key-value pairs
89
89
auto response = httpRequest
90
- .addHeader (" Custom-Header1" , " value1" )
91
- .addHeader (" Custom-Header2" , " value2" )
92
- .send ()
93
- .get ();
90
+ .addHeader (" Custom-Header1" , " value1" )
91
+ .addHeader (" Custom-Header2" , " value2" )
92
+ .send ()
93
+ .get ();
94
94
95
95
std::cout << " Succeed: " << response.succeed << std::endl;
96
96
}
97
97
98
- void simplePostWithFormData () {
99
-
98
+ void simplePostWithFormData ()
99
+ {
100
100
HttpRequest httpRequest (" https://httpbun.com/post" );
101
101
102
102
// You can send a POST request with form data in the payload
103
103
auto response = httpRequest
104
- .setMethod (HttpMethod::POST)
105
- .setPayload (" param1=7¶m2=test" )
106
- .send ()
107
- .get ();
104
+ .setMethod (HttpMethod::POST)
105
+ .setPayload (" param1=7¶m2=test" )
106
+ .send ()
107
+ .get ();
108
108
109
109
std::cout << " Succeed: " << response.succeed << std::endl;
110
110
std::cout << " Http Status Code: " << response.statusCode << std::endl;
111
111
std::cout << " Data: " << response.textData << std::endl;
112
112
}
113
113
114
- void simplePostWithJSONData () {
115
-
114
+ void simplePostWithJSONData ()
115
+ {
116
116
HttpRequest httpRequest (" https://httpbun.com/post" );
117
117
118
118
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
119
119
auto response = httpRequest
120
- .setMethod (HttpMethod::POST)
121
- .setPayload (R"( {"param1": 7, "param2": "test"})" )
122
- .addHeader (" Content-Type" , " application/json" )
123
- .send ()
124
- .get ();
120
+ .setMethod (HttpMethod::POST)
121
+ .setPayload (R"( {"param1": 7, "param2": "test"})" )
122
+ .addHeader (" Content-Type" , " application/json" )
123
+ .send ()
124
+ .get ();
125
125
126
126
std::cout << " Succeed: " << response.succeed << std::endl;
127
127
std::cout << " Http Status Code: " << response.statusCode << std::endl;
128
128
std::cout << " Data: " << response.textData << std::endl;
129
129
}
130
130
131
- void simplePutWithFormData () {
132
-
131
+ void simplePutWithFormData ()
132
+ {
133
133
HttpRequest httpRequest (" https://httpbun.com/put" );
134
134
135
135
// You can send a PUT request with form data in the payload just like POST
136
136
auto response = httpRequest
137
- .setMethod (HttpMethod::PUT)
138
- .setPayload (" param1=7¶m2=test" )
139
- .send ()
140
- .get ();
137
+ .setMethod (HttpMethod::PUT)
138
+ .setPayload (" param1=7¶m2=test" )
139
+ .send ()
140
+ .get ();
141
141
142
142
std::cout << " Succeed: " << response.succeed << std::endl;
143
143
std::cout << " Http Status Code: " << response.statusCode << std::endl;
144
144
std::cout << " Data: " << response.textData << std::endl;
145
145
}
146
146
147
- void simpleDeleteWithFormData () {
148
-
147
+ void simpleDeleteWithFormData ()
148
+ {
149
149
HttpRequest httpRequest (" https://httpbun.com/delete" );
150
150
151
151
// You can send a DELETE request with form data in the payload just like POST
152
152
auto response = httpRequest
153
- .setMethod (HttpMethod::DELETE_)
154
- .setPayload (" param1=7¶m2=test" )
155
- .send ()
156
- .get ();
153
+ .setMethod (HttpMethod::DELETE_)
154
+ .setPayload (" param1=7¶m2=test" )
155
+ .send ()
156
+ .get ();
157
157
158
158
std::cout << " Succeed: " << response.succeed << std::endl;
159
159
std::cout << " Http Status Code: " << response.statusCode << std::endl;
160
160
std::cout << " Data: " << response.textData << std::endl;
161
161
}
162
162
163
- void simplePatch () {
164
-
163
+ void simplePatch ()
164
+ {
165
165
HttpRequest httpRequest (" https://httpbun.com/patch" );
166
166
167
167
// You can send a PATCH request with QueryString just like GET
168
168
auto response = httpRequest
169
- .setMethod (HttpMethod::PATCH)
170
- .setQueryString (" param1=7¶m2=test" )
171
- .send ()
172
- .get ();
169
+ .setMethod (HttpMethod::PATCH)
170
+ .setQueryString (" param1=7¶m2=test" )
171
+ .send ()
172
+ .get ();
173
173
174
174
std::cout << " Succeed: " << response.succeed << std::endl;
175
175
std::cout << " Http Status Code: " << response.statusCode << std::endl;
176
176
std::cout << " Data: " << response.textData << std::endl;
177
177
}
178
178
179
- void ignoreSslErrors () {
180
-
179
+ void ignoreSslErrors ()
180
+ {
181
181
HttpRequest httpRequest (" https://self-signed-cert.httpbun.com" );
182
182
183
183
// If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
@@ -188,30 +188,48 @@ void ignoreSslErrors() {
188
188
std::cout << " Data: " << response.textData << std::endl;
189
189
}
190
190
191
+ void setUploadAndDownloadBandwidthLimit ()
192
+ {
193
+ HttpRequest httpRequest (" https://httpbun.com/get" );
191
194
192
- int main () {
193
-
194
- simpleGet ();
195
-
196
- nonBlockingGet ();
197
-
198
- receiveBinaryData ();
199
-
200
- receiveError ();
201
-
202
- sendingHttpHeaders ();
203
-
204
- simplePostWithFormData ();
205
-
206
- simplePostWithJSONData ();
207
-
208
- simplePutWithFormData ();
195
+ // You can set the upload and download bandwidth limit in bytes per second
196
+ auto response = httpRequest
197
+ .setUploadBandwidthLimit (20480 ) // 2 KB/sec
198
+ .setDownloadBandwidthLimit (10240 ) // 1 KB/sec
199
+ .send ()
200
+ .get ();
209
201
210
- simpleDeleteWithFormData ();
202
+ std::cout << " Succeed: " << response.succeed << std::endl;
203
+ std::cout << " Http Status Code: " << response.statusCode << std::endl;
204
+ std::cout << " Data: " << response.textData << std::endl;
205
+ }
211
206
212
- simplePatch ();
213
207
214
- ignoreSslErrors ();
208
+ int main ()
209
+ {
210
+ // simpleGet();
211
+ //
212
+ // nonBlockingGet();
213
+ //
214
+ // receiveBinaryData();
215
+ //
216
+ // receiveError();
217
+ //
218
+ // sendingHttpHeaders();
219
+ //
220
+ // simplePostWithFormData();
221
+ //
222
+ // simplePostWithJSONData();
223
+ //
224
+ // simplePutWithFormData();
225
+ //
226
+ // simpleDeleteWithFormData();
227
+ //
228
+ // simplePatch();
229
+ //
230
+ // ignoreSslErrors();
231
+
232
+ setUploadAndDownloadBandwidthLimit ();
215
233
216
234
return 0 ;
217
- }
235
+ }
0 commit comments