Updating Variable in JSON Request Body using Code

Aplogies in advance, pretty new to postman and not much cop at coing.

I am currently putting a number of bookings through to a system where let’s say the variables examples are; BookingRefOne and BookingRefTwo. These could increment with each JSON Object in the response.

[{

"ProviderId": 25,

" BookingRef ": "{{ BookingRefOne}}"

}, {

"ProviderId": 25,

" BookingRef ": "{{ BookingRefTwo}}" //Set at collection Level

}

]

I am populating the above by running the below pre request scripts and setting variables.

let bookingRefIncrement **=** pm.globals.**get** ("bookingRefIncrement")

pm.globals.**set** ("bookingRefIncrement", **+** bookingRefIncrement **+** 1)

const bookingRefIncrement **=** pm.globals.**get** ("bookingRefShortName") **+** pm.globals.**get** ("bookingRefIncrement")

pm.globals.**set** ("bookingRefIncrement", bookingRefIncrement)

pm.globals.**set** ("bookingRefOne", pm.globals.**get** ("bookingRefShortName") **+** bookingRefIncrement**++** )

pm.globals.**set** ("bookingRefIncrement", **+** bookingRefIncrement **+** 1)

pm.globals.**set** ("bookingRefTwo", pm.globals.**get** ("bookingRefShortName") **+** providerReferenceIncrement**++** )

pm.globals.**set** ("bookingRefIncrement", **+** bookingRefIncrement**+** 1)

Each time I want to add a new JSON object to the request body, I have to add these lines and manually change the references in the pre request scripts.

pm.globals.set (“bookingRefOne”, pm.globals.get (“bookingRefShortName”) + bookingRefIncrement**++** )

pm.globals.set (“bookingRefIncrement”, + bookingRefIncrement + 1)

I am not very good at this so I am surprised I got this far, is there an easy to understand way/better way to do this with a few lines of code, where the pre request script could look at the request body and inject these incrementing them for me.

For information, the booking ref is made up of static text e.g., “Booking “ + bookingRefIncrement’ so it might b Booking 1, Booking 2 and so for each object within the request body.

hope this makes sense as I don’t know how better to explain it.

If anybody could help me, or point me in the right direction that would be great.

First of all, I wouldn’t use global variables as these are available to all collections in Postman and I’m not sure that is your intention. Best off using an collection variable for non-confidential information, and Environment variables for confidential stuff.

However, I would approach this slightly different.

I would have an collection or environment variable that contains an array of booking refs. If you want to add a booking ref, you would just add it to the array.

You would setup the request as RAW\JSON, but don’t put anything in the body.

image

You are going to generate the JSON body in the pre-request scripts.

Something like the following…

// define booking refs, or read from a collection or environment variable
let bookingrefs = ["bookingRefShortName1", "bookingRefShortName2", "bookingRefShortName3"]

// define blank array for request body.
let body = []

// loop through the booking refs, create an object for each entry, and then push to array
bookingrefs.forEach(booking => {
    let ProviderId = 25
    let newObj = {
        "ProviderId": ProviderId,
        "Booking Ref": booking
    }
    body.push(newObj)
});

// set the array as the new body for the request
pm.request.body.raw = JSON.stringify(body);

This will add an object for each booking, so adding or removing entries should only need you to update the booking array.

You can check the console log to see what was actually sent.

1 Like

Thanks I will take a look at this.
I had planned to move things into Collection Level, its been very much learn as you go.

Will update on how I get on when I find the time to try implement it.

Out of interest is there a way to easilly make this generate the booking refs by using a loop e.g. loop x amount of time and increment the booking ref by 1 each time so it adds to the array itself?

Just pondering if I can do a round of testing and clean up all the refs at the end without having to constanty delete them (they need to be unique for most of this stuff, later obliterating the DB entries manually.

Something like…

let bookingRefs = [];
let N = 5;

for (var i = 1; i <= N; i++) {
   bookingRefs.push("bookingRef" + i);
}

console.log(bookingRefs);

Good practice with automated tests is to clean up after yourself, so your tear down script should clean up those booking ref’s if at all possible.

You can reset the database, and that can be automated, but this doesn’t really lend itself to continuous integration where you should be able to run the test as frequent and whenever you want.

Even better is to check this before you run the test, rather than rely on the state of a previously run test.

I don’t know much about the database that your API is using, but a lot of databases now have their own API’s to allow you to delete data before you run your tests. To ensure the system is in a known state. You can have tests in your pre-request script to ensure that the known state is correct.

1 Like

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