mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 06:26:02 -07:00
Better error handling on client (#601)
This commit is contained in:
parent
cf084e1db1
commit
dc5f9ba164
5 changed files with 727 additions and 657 deletions
23
README.md
23
README.md
|
@ -286,9 +286,13 @@ int main(void)
|
||||||
{
|
{
|
||||||
httplib::Client cli("localhost", 1234);
|
httplib::Client cli("localhost", 1234);
|
||||||
|
|
||||||
auto res = cli.Get("/hi");
|
if (auto res = cli.Get("/hi")) {
|
||||||
if (res && res->status == 200) {
|
if (res->status == 200) {
|
||||||
std::cout << res->body << std::endl;
|
std::cout << res->body << std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auto err = res.error();
|
||||||
|
...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -434,13 +438,12 @@ auto res = cli_.Post(
|
||||||
httplib::Client client(url, port);
|
httplib::Client client(url, port);
|
||||||
|
|
||||||
// prints: 0 / 000 bytes => 50% complete
|
// prints: 0 / 000 bytes => 50% complete
|
||||||
std::shared_ptr<httplib::Response> res =
|
auto res = cli.Get("/", [](uint64_t len, uint64_t total) {
|
||||||
cli.Get("/", [](uint64_t len, uint64_t total) {
|
printf("%lld / %lld bytes => %d%% complete\n",
|
||||||
printf("%lld / %lld bytes => %d%% complete\n",
|
len, total,
|
||||||
len, total,
|
(int)(len*100/total));
|
||||||
(int)(len*100/total));
|
return true; // return 'false' if you want to cancel the request.
|
||||||
return true; // return 'false' if you want to cancel the request.
|
}
|
||||||
}
|
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -23,12 +23,12 @@ int main(void) {
|
||||||
httplib::Client cli("localhost", 8080);
|
httplib::Client cli("localhost", 8080);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto res = cli.Get("/hi");
|
if (auto res = cli.Get("/hi")) {
|
||||||
if (res) {
|
|
||||||
cout << res->status << endl;
|
cout << res->status << endl;
|
||||||
cout << res->get_header_value("Content-Type") << endl;
|
cout << res->get_header_value("Content-Type") << endl;
|
||||||
cout << res->body << endl;
|
cout << res->body << endl;
|
||||||
} else {
|
} else {
|
||||||
|
cout << "error code: " << res.error() << std::endl;
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
auto result = cli.get_openssl_verify_result();
|
auto result = cli.get_openssl_verify_result();
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|
|
@ -17,12 +17,12 @@ int main(void) {
|
||||||
auto scheme_host_port = "http://localhost:8080";
|
auto scheme_host_port = "http://localhost:8080";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto res = httplib::Client(scheme_host_port).Get("/hi");
|
if (auto res = httplib::Client(scheme_host_port).Get("/hi")) {
|
||||||
|
|
||||||
if (res) {
|
|
||||||
cout << res->status << endl;
|
cout << res->status << endl;
|
||||||
cout << res->get_header_value("Content-Type") << endl;
|
cout << res->get_header_value("Content-Type") << endl;
|
||||||
cout << res->body << endl;
|
cout << res->body << endl;
|
||||||
|
} else {
|
||||||
|
cout << res.error() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
408
test/test.cc
408
test/test.cc
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue