Skip to content

Commit 49e2a79

Browse files
Merge pull request #29 from lk-libs/timeout
Timeout
2 parents 0af9372 + 6d6a380 commit 49e2a79

File tree

4 files changed

+159
-90
lines changed

4 files changed

+159
-90
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++
2424
* [POST request with JSON data](#post-request-with-json-data)
2525
* [What about others? (PUT, DELETE, PATCH)](#what-about-others-put-delete-patch)
2626
* [How to ignore SSL certificate errors?](#how-to-ignore-ssl-certificate-errors)
27+
* [How to set timeout?](#how-to-set-timeout)
2728
* [How can I limit download and upload bandwidth?](#how-can-i-limit-download-and-upload-bandwidth)
2829
* [Semantic Versioning](#semantic-versioning)
2930
* [Full function list](#full-function-list)
@@ -387,6 +388,29 @@ int main() {
387388
}
388389
```
389390

391+
## How to set timeout?
392+
393+
You can use the setTimeout method to set the timeout duration in seconds during requests.
394+
395+
```cpp
396+
#include <fstream>
397+
#include "libcpp-http-client.hpp"
398+
399+
using namespace lklibs;
400+
401+
int main() {
402+
HttpRequest httpRequest("https://api.myproject.com");
403+
404+
// You can set the timeout in seconds
405+
auto response = httpRequest
406+
.setTimeout(3) // 3 sec
407+
.send()
408+
.get();
409+
410+
return 0;
411+
}
412+
```
413+
390414

391415
## How can I limit download and upload bandwidth?
392416

examples/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ void ignoreSslErrors()
187187
std::cout << "Data: " << response.textData << std::endl;
188188
}
189189

190+
void setTimeout()
191+
{
192+
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
193+
194+
// You can set the timeout in seconds
195+
auto response = httpRequest
196+
.setTimeout(3) // 3 sec
197+
.send()
198+
.get();
199+
200+
std::cout << "Succeed: " << response.succeed << std::endl;
201+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
202+
std::cout << "Error Message: " << response.errorMessage << std::endl;
203+
}
204+
190205
void setDownloadAndUploadBandwidthLimit()
191206
{
192207
HttpRequest httpRequest("https://httpbun.com/get");
@@ -228,6 +243,8 @@ int main()
228243

229244
ignoreSslErrors();
230245

246+
setTimeout();
247+
231248
setDownloadAndUploadBandwidthLimit();
232249

233250
return 0;

0 commit comments

Comments
 (0)