How to rerun the same request a few times

hi, this is my script, I am trying to rerun the same request IF the response is 206 but it’s not working so what i am missing?
Please advise.
Thanks

// Library function to handle general HTTP response validation
Library_206HTTPresponse_validation = () => 
{
    // Check if the response status code is 206
    if (pm.response.code === 206) 
    {
        // Initialize the retry count if not set
        if (!pm.iterationData.get('retryCount')) 
        {
            pm.iterationData.set('retryCount', 1);
        }

        // Check if the maximum number of retries (5 in this case) has been reached
        if (pm.iterationData.get('retryCount') <= 5) 
        {
            // Log the retry attempt
            console.log('Retrying the request. Attempt #' + pm.iterationData.get('retryCount'));

            // Increment the retry count
            pm.iterationData.set('retryCount', pm.iterationData.get('retryCount') + 1);

            // Rerun the request
            postman.setNextRequest(pm.info.requestName);
            
            // Return only if it's a retry attempt
            return;
        } 
        else 
        {
            // Log that the maximum number of retries has been reached
            console.log('Maximum retries reached. Aborting further attempts.');
        }
    } 
    else 
    {
        // If the response code is not 206, perform general HTTP response validation
        try {
            // Parse the JSON response
            var jsonData = pm.response.json();

            // Handle different response status codes
            switch (pm.response.code) 
            {
                case 200:
                    console.log('HTTP 200 OK πŸ‘');
                    break;
                case 404:
                    console.error('Received a 404 status code, ❌');
                    // Do something specific for 404 errors, if needed
                    break;
                case 500:
                    console.error('Received a 500 status code. Server is kaput. ❌');
                    break;
                default:
                    console.warn('Unhandled HTTP response code:', pm.response.code);
            }
        } 
        catch (e) {
            console.error('Error parsing JSON response:', e);

            // If the response is 404, handle it differently
            if (pm.response.code === 404) {
                console.error('Received a 404 status code. ❌');
                // Do something specific for 404 errors, if needed
            } else {
                // For other errors, fail the request with a 500 status code and a JSON error message
                pm.response.status = 500;
                pm.response.json({ error: 'Server is broken, you\'re getting a response of 500!!! ❌❌❌❌' });
            }
        }
    }
};

// Set the library function in the global variables
pm.globals.set('Library_206HTTPresponse_validation', Library_206HTTPresponse_validation.toString());

Are you getting any error messages?

You might want to add some console logs for troubleshooting purposes.

The iteration data is read only as far as I’m aware, so you can’t do this.

pm.iterationData.set('retryCount', 1);

Are you actually running this with a data file. Should that just be a collection variable instead?

Finally, setNextRequest only works with the collection runner.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.