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