[SOLVED] Cannot pass a string array from external data file postman automation scripts

Issue

I’m trying to pass a string array in postman- post request

postman request- body

{
"fruits":[{{fruits}}],

 } 

postman- test

var fruits=["mango","apple","orange"]
for (let i = 0; i < 3; i++) {
fruits.push(jsonData.fruits.toString());
}

pm.globals.set("fruits", fruits);

Then set fruits as global variables
gloabal variables

Then run the API through the collection runner with the external data file. Then check the response body and I got this
can't pass the array as string

got a 404 error

If anyone can let me know how to pass a array as a string in postman. When we pass a string array it will defined as a ascii value. So this issue has happened. So please guide me through this.

Solution

First, if you want to save an array as a variable, remember to stringify this.

let fruits=["mango","apple","orange"]
pm.globals.set("fruits", JSON.stringify(fruits));

Second, you don’t need to add [ ] in request body, just put the variable

{
    "fruits": {{fruits}}
}

Result:

{
    "fruits": ["mango","apple","orange"]
}

Answered By – lucas-nguyen-17

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *