JSON data driven test: replace entire request

Hello,

I’d appreciate some help, please.

I am data driving a test using a JSON source file. Instead of replacing single variables at run time, is it possible to replace the entire request body with the one from the data file?

So, for example, if my data looked like the screenshot below, could I use the ‘message’ object as the entire request body?

image

Thanks in advance.

1 Like

This tutorial from @vdespa might be something that will help describe how you can do it:

2 Likes

@dannydainton Danny, you (and @vdespa Valentin) are lifesavers! Thank you - that’s worked perfectly.

2 Likes

Hi,

I followed this tutorial, but was still getting [object Object] in the Request Body when running my test through the collection runner (now am getting ‘Data unavailable’, which I’ve opened another question about).

This is what I have:
Request Body:
{{guest}}

Pre-request script:

let newGuest = pm.iterationData.get('guest');

if (pm.iterationData.has('guest')) {
    console.log(JSON.stringify(newGuest)) // successfully prints out what's expected
    pm.environment.set('guest', JSON.stringify(newGuest)); // doesn't set variable properly
}

printing pm.environment.get('guest') prints what I would expect

Appreciate any help!

As there are now 2 variables at different scopes (data and environment), with the same name, these might be clashing.

Try changing the name of the environment variable from ‘guest’ to ‘newGuest’ and the value in the request body to {{newGuest}}. Ensure to save the request before running this in the Collection Runner.

These names are just suggestions, you can call it whatever you like :grin:

1 Like

this worked! thank you! and it looks like it got rid of the other issue i had when viewing Request Body as well.

1 Like