Added try catch to example synchronous client requests

This commit is contained in:
eidheim 2017-07-22 14:02:03 +02:00
commit 9548404fb4
2 changed files with 24 additions and 12 deletions

View file

@ -226,13 +226,19 @@ int main() {
// Client examples
HttpClient client("localhost:8080");
string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
// Synchronous request examples
try {
auto r1 = client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
auto r2 = client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
}
catch(const SimpleWeb::system_error &e) {
cerr << "Client request error: " << e.what() << endl;
}
// Asynchronous request example
client.request("POST", "/json", json_string, [](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {

View file

@ -225,13 +225,19 @@ int main() {
// Second create() parameter set to false: no certificate verification
HttpsClient client("localhost:8080", false);
string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
// Synchronous request examples
try {
auto r1 = client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
auto r2 = client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
}
catch(const SimpleWeb::system_error &e) {
cerr << "Client request error: " << e.what() << endl;
}
// Asynchronous request example
client.request("POST", "/json", json_string, [](shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) {