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());