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
Then run the API through the collection runner with the external data file. Then check the response body and I got this
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.
CodePudding user response:
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"]
}