Skip to content

Commit 5c64de3

Browse files
committed
Update README and examples
1 parent 81e5e12 commit 5c64de3

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ The following example demonstrates an `HTTP GET` request with JSON parsing provi
1414
int main() {
1515
SimpleHttp::Client client;
1616
SimpleHttp::HttpUrl url{"https://jsonplaceholder.typicode.com/users"};
17-
const std::optional<SimpleHttp::HttpResponse> &maybeResponse = client.get(url);
18-
19-
if (maybeResponse) {
20-
auto parsed = nlohmann::json::parse(maybeResponse.value().body.value());
21-
std::cout << parsed[0]["id"] << std::endl;
22-
} else {
23-
std::cout << "Failed GET for: " << url.value() << std::endl;
24-
}
17+
client.get(url).template match<void>(
18+
[&url](const SimpleHttp::HttpFailure &failure){
19+
std::cout << "Failed GET for: " << url.value() << std::endl;
20+
},
21+
[](const SimpleHttp::HttpSuccess &success){
22+
auto parsed = nlohmann::json::parse(success.body().value());
23+
std::cout << parsed[0]["id"] << std::endl;
24+
}
25+
);
2526
}
2627
```
2728

example/jsonplaceholder/main.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
int main() {
66
SimpleHttp::Client client;
77
SimpleHttp::HttpUrl url{"https://jsonplaceholder.typicode.com/users"};
8-
const std::optional<SimpleHttp::HttpResponse> &maybeResponse = client.get(url);
9-
10-
if (maybeResponse) {
11-
auto parsed = nlohmann::json::parse(maybeResponse.value().body.value());
12-
std::cout << parsed[0]["id"] << std::endl;
13-
} else {
14-
std::cout << "Failed GET for: " << url.value() << std::endl;
15-
}
8+
client.get(url).template match<void>(
9+
[&url](const SimpleHttp::HttpFailure &failure){
10+
std::cout << "Failed GET for: " << url.value() << std::endl;
11+
},
12+
[](const SimpleHttp::HttpSuccess &success){
13+
auto parsed = nlohmann::json::parse(success.body().value());
14+
std::cout << parsed[0]["id"] << std::endl;
15+
}
16+
);
1617
}

0 commit comments

Comments
 (0)