Retry http requests

Is there a way using k6/http lib to retry http requests returning specific response codes?
Say, I’d like to retry for up to 3 times all requests that return a 408 or 5xx.

There is no such functionality in k6, but you can add it fairly simply by wrapping the k6/http functions like:

function httpGet(url, params) {
    var res; 
    for (var retries = 3; retries > 0; retries--) {
        res = http.get(url, params)
        if (res.status != 408 && res.status < 500) {
            return res;
        }
    }
    return res;

}

And then just use httpGet instead of http.get :wink:

the above approach looks fine when the response code is not in certain range. Is there any reasonable way to retry requests that timeout ? is there any built in mechanism in k6 (not looking for extending the default request timeout from 60 seconds)

Hi @jeevananthank,
If the request timeouts the status code will be 0 so you can just check for that as well.
Hope this helps

1 Like