cofetch
Chainable, high-performance async HTTP client for C++ event loops
Loading...
Searching...
No Matches
example_errors.cpp

Transport errors (error_code) vs HTTP error statuses (is_ok()).

Transport errors (error_code) vs HTTP error statuses (is_ok()).

// Two different kinds of failure, deliberately kept apart:
// - transport errors (DNS, TLS, timeout) arrive in the error_code
// - HTTP error statuses are *successful* transfers; check res.is_ok()
#include <cofetch.h>
#include <asio.hpp>
#include <iostream>
int main() {
asio::io_context io;
cofetch::Client http(io);
// DNS failure: ec is set, the response carries the curl error text.
http.async_get("https://no-such-host.invalid/",
[](std::error_code ec, const cofetch::Response&) {
std::cout << "transport error: " << ec.message() << "\n";
});
// 404: the transfer worked, the server just said no.
http.async_get("https://postman-echo.com/status/404",
[](std::error_code ec, const cofetch::Response& res) {
if (!ec && !res.is_ok()) {
std::cout << "http status: " << res.http_code_ << "\n";
}
});
io.run();
}
Definition cofetch.h:266
auto async_get(std::string url, CompletionToken &&token)
Definition cofetch.h:324
Definition cofetch.h:111
bool is_ok() const
True when the transfer succeeded and the HTTP status is 2xx.
Definition cofetch.h:128