Set array from environment variable for request body in postman

Hey @Ra_Na.

You could stringify and set the entire request body in an environment variable and then simply use that in the request body for the types to work correctly.

This is how it would look.

  • Pre request script
let flavor,
    requestBody = {
        "connection": "string",
        "name": "string",
    };

if (os.toLowerCase() === "windows") {
    flavor= ["hello","hi"];
}
else {
    flavor = ["hello","hi","how do you do"];
}

requestBody.type = flavor;

pm.environment.set("body", JSON.stringify(requestBody));
  • Request body - raw
{{body}}

Hope this helps.

2 Likes