Automate Post request with different variables in the body and save to a file

Hi All , I hope you can help me

Im trying to query a website , which seems to work nice with a single request however I would like to now automate this procedure due to the amount of records I need to return ( 100’s )

For example I would like to:

Automate POST with a body of a different ID in each request ( I have a list of ID’s I want to query , Ideally I’d want postman to cycle through the list and record the result for each ID per POST request)

eg.
POST 1. ID = 1231
POST 2. ID = 1232
POST 3. ID = 1233
POST 4. ID = 1234

Record each response and extract the result and store it in a single file togther with the BODY variable in the POST.

POST 1. ID = 1231. REPONSE = RED CAR

POST 2. ID = 1232. REPONSE = BLUE CAR

POST 3. ID = 1233. REPONSE = PURPLE CAR

POST 4. ID = 1234. REPONSE = GREY CAR

Can someone please explain how I can achieve this with some examples ?

A virtual beer is ready for the willing participant :slight_smile:

Much appreciated.

The first part of your request is reasonably straight forward, but you can’t write directly to disk with Postman, so that part will be more difficult. I would recommend setting up a database with API access to send the results to.

The first part of your request can be achieved using the collection runner and setNextRequest to loop through an array of test data (your ID’s).

Setup three collection variables to start with. You need a blank array called “IDs”, a blank object to store your results, and finally a variable to store the count (which will be used to custom tailor the results).

My example is running against Postman echo, and is just sending the ID as part of the URL.

https://postman-echo.com/get?test={{id}}

The pre-request script gets retrieves the IDs array set earlier, and if its blank will repopulate it with what ever test data you add.

If the array is zero, it will also reset the results object and count.

It uses a JavaScript function called shift() which will retrieve the first element in the array (and also remove it from the array).

The script finally sets the currentID and also updates the saved collectionVariable with the updated IDs array.

var array = JSON.parse(pm.collectionVariables.get("IDs"));

if(array.length === 0){
    array = [1231, 1232, 1233];
    // also reset the results object and count variable
    pm.collectionVariables.set("results", JSON.stringify({}));
    pm.collectionVariables.set("count", 1);
}

var currentID = array.shift();

pm.collectionVariables.set("id", currentID);
pm.collectionVariables.set("IDs", JSON.stringify(array));

In the Tests tab, you retrieve the IDs array again so you can keep looping until the array is zero length. You also retrieve the current results and count.

It has a simple test which just checks if the response is the same as what was sent.

If the test passes, then the results are updated, the count increased and both collectionVariables are updated.

There is an IF statement that will keep running the same request until the array length is zero.

The ELSE statement is where the results are finally logged to the Console. Its here where you would need to send your results to a database or API.

let array = JSON.parse(pm.collectionVariables.get("IDs"));
let currentResults = JSON.parse(pm.collectionVariables.get("results"));
let currentCount = pm.collectionVariables.get("count");

const expectedResponse = pm.collectionVariables.get("id");
const actualResponse = pm.response.json().args.test;

pm.test(`ID = ${expectedResponse}`, () => {
    pm.expect(expectedResponse).to.eql(parseInt(actualResponse)); 
    // add to results object
    currentResults[`POST${currentCount}`] = actualResponse;
    // increase current count
    currentCount++;
    // write results and current count back to collection variables
    pm.collectionVariables.set("results", JSON.stringify(currentResults));
    pm.collectionVariables.set("count", currentCount);
});

if (array.length > 0){
    postman.setNextRequest("Postman Echo Loop");
} else {
    console.log(JSON.parse(pm.collectionVariables.get("results")));
    // this is where you will need to send the results to another API as you can't write directly to disk
    postman.setNextRequest(null);
}

image

Thanks very much for this reply , I have not had a chance to test this yet , however I will do in the next couple of days… I will let you know if that has worked for me.

I really appreciate your detailed response… Have a virtual beer on me! :slight_smile: