33
44using namespace lklibs ;
55
6- void simpleGet () {
7-
6+ void simpleGet ()
7+ {
88 HttpRequest httpRequest (" https://httpbun.com/get" );
99
1010 // The simplest but slowest method if multiple calls will be made
1111 auto response = httpRequest
12- .setQueryString (" param1=7¶m2=test" )
13- .send ()
14- .get ();
12+ .setQueryString (" param1=7¶m2=test" )
13+ .send ()
14+ .get ();
1515
1616 std::cout << " Succeed: " << response.succeed << std::endl;
1717 std::cout << " Http Status Code: " << response.statusCode << std::endl;
1818 std::cout << " Data: " << response.textData << std::endl;
1919}
2020
21- void nonBlockingGet () {
22-
21+ void nonBlockingGet ()
22+ {
2323 HttpRequest httpRequest1 (" https://httpbun.com/get" );
2424 HttpRequest httpRequest2 (" https://httpbun.com/get" );
2525 HttpRequest httpRequest3 (" https://httpbun.com/get" );
@@ -47,15 +47,15 @@ void nonBlockingGet() {
4747 std::cout << " Response3 Data: " << response3.textData << std::endl;
4848}
4949
50- void receiveBinaryData () {
51-
50+ void receiveBinaryData ()
51+ {
5252 HttpRequest httpRequest (" https://httpbun.com/bytes/100" );
5353
5454 // If you need to retrieve binary data such as an image, just call the "returnAsBinary" method before send
5555 auto response = httpRequest
56- .returnAsBinary ()
57- .send ()
58- .get ();
56+ .returnAsBinary ()
57+ .send ()
58+ .get ();
5959
6060 std::cout << " Succeed: " << response.succeed << std::endl;
6161 std::cout << " Http Status Code: " << response.statusCode << std::endl;
@@ -64,8 +64,8 @@ void receiveBinaryData() {
6464 std::cout << " Data Size: " << response.binaryData .size () << std::endl;
6565}
6666
67- void receiveError () {
68-
67+ void receiveError ()
68+ {
6969 HttpRequest httpRequest (" https://httpbun.com/not_found" );
7070
7171 // This is an exception free library. If an error occurs, no exception is thrown
@@ -81,103 +81,103 @@ void receiveError() {
8181 std::cout << " Error Message: " << response.errorMessage << std::endl;
8282}
8383
84- void sendingHttpHeaders () {
85-
84+ void sendingHttpHeaders ()
85+ {
8686 HttpRequest httpRequest (" https://httpbun.com/get?param1=7¶m2=test" );
8787
8888 // You can send custom headers as key-value pairs
8989 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 ();
9494
9595 std::cout << " Succeed: " << response.succeed << std::endl;
9696}
9797
98- void simplePostWithFormData () {
99-
98+ void simplePostWithFormData ()
99+ {
100100 HttpRequest httpRequest (" https://httpbun.com/post" );
101101
102102 // You can send a POST request with form data in the payload
103103 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 ();
108108
109109 std::cout << " Succeed: " << response.succeed << std::endl;
110110 std::cout << " Http Status Code: " << response.statusCode << std::endl;
111111 std::cout << " Data: " << response.textData << std::endl;
112112}
113113
114- void simplePostWithJSONData () {
115-
114+ void simplePostWithJSONData ()
115+ {
116116 HttpRequest httpRequest (" https://httpbun.com/post" );
117117
118118 // You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
119119 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 ();
125125
126126 std::cout << " Succeed: " << response.succeed << std::endl;
127127 std::cout << " Http Status Code: " << response.statusCode << std::endl;
128128 std::cout << " Data: " << response.textData << std::endl;
129129}
130130
131- void simplePutWithFormData () {
132-
131+ void simplePutWithFormData ()
132+ {
133133 HttpRequest httpRequest (" https://httpbun.com/put" );
134134
135135 // You can send a PUT request with form data in the payload just like POST
136136 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 ();
141141
142142 std::cout << " Succeed: " << response.succeed << std::endl;
143143 std::cout << " Http Status Code: " << response.statusCode << std::endl;
144144 std::cout << " Data: " << response.textData << std::endl;
145145}
146146
147- void simpleDeleteWithFormData () {
148-
147+ void simpleDeleteWithFormData ()
148+ {
149149 HttpRequest httpRequest (" https://httpbun.com/delete" );
150150
151151 // You can send a DELETE request with form data in the payload just like POST
152152 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 ();
157157
158158 std::cout << " Succeed: " << response.succeed << std::endl;
159159 std::cout << " Http Status Code: " << response.statusCode << std::endl;
160160 std::cout << " Data: " << response.textData << std::endl;
161161}
162162
163- void simplePatch () {
164-
163+ void simplePatch ()
164+ {
165165 HttpRequest httpRequest (" https://httpbun.com/patch" );
166166
167167 // You can send a PATCH request with QueryString just like GET
168168 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 ();
173173
174174 std::cout << " Succeed: " << response.succeed << std::endl;
175175 std::cout << " Http Status Code: " << response.statusCode << std::endl;
176176 std::cout << " Data: " << response.textData << std::endl;
177177}
178178
179- void ignoreSslErrors () {
180-
179+ void ignoreSslErrors ()
180+ {
181181 HttpRequest httpRequest (" https://self-signed-cert.httpbun.com" );
182182
183183 // If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
@@ -188,30 +188,48 @@ void ignoreSslErrors() {
188188 std::cout << " Data: " << response.textData << std::endl;
189189}
190190
191+ void setUploadAndDownloadBandwidthLimit ()
192+ {
193+ HttpRequest httpRequest (" https://httpbun.com/get" );
191194
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 ();
209201
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+ }
211206
212- simplePatch ();
213207
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 ();
215233
216234 return 0 ;
217- }
235+ }
0 commit comments