Conditional JSON payload based on csv variables

@mrosi
Here’s what you can do:

  1. Build a dynamic request body, what I mean by that is you need to build the request body from the pre-request script, and make it a variable something like this:

  2. So every iteration in the runner will get the corresponding data from the csv file.
    To access the current iteration data you can use the pm.iterationData API in your pre-request script:

let currentData = pm.iterationData.toJSON(),
 dynamicRequestBody = {};

// Now let's build the body using conditionals etc:
if (currentData.IP_ANYCAST) {
  dynamicRequestBody = currentData;
}
else {
  dynamicRequestBody = { name: currentData.NAME };
}

pm.variables.set('dynamicRequestBody', JSON.stringify(dynamicRequestBody));

This is just an example on how you can dynamically build the request body using the pre-request script with the help of local variables.

Now, based on your conditions you can build this body and store it as a local variable and during the request execution, this variable will get resolved and thus you can make the API call based on your CSV data.

1 Like