Retry on failure

It's always good to give some more chances to the same action, this can easily be achieved by wrapping the request calls :

on_response handler

We can use the indirection offered by std::function, so that the request and the on_response handler can respectively refers each other.

auto retries_count = 3;
std::function<void(xxhr::Response&&)> retry_on_fail;

define the request

auto do_request = [&]() {
GET(
"http://httpbin.org/post"s,
on_response = retry_on_fail
);
};

on_response definition

Now we can write an on_response handler which will calls the request again if it failed and there are still retries_count available.

retry_on_fail = [&](auto&& resp) {
if ( resp.error && (retries_count > 0) ) {
--retries_count;
do_request();
} else {
// correct response
}
};

Don't forget to initiate the first try

do_request();
xxhr::GET
void GET(Ts &&... ts)
HTTP GET Request.
Definition: api.hpp:41
xxhr::Response
Passed to the unary callback xxhr::on_response. Provides access to request Content and Success,...
Definition: response.hpp:18