Erandi Dilshani
Erandi Dilshani

Reputation: 355

Postman returns [object object ] and value is not passing

I'm working with postman automation

There is a vehicles array in the body

{
   "Vehicles":[
      {
         "car":"{{car}}",
         "bike":"{{bike}}"
      }
   ]
}

I need to change this like below

{
   "Vehicles":"[{{vehicles}}]"
}

So I have created a pre=request script

let car, bike;
var vehicles = {
    car: data.vehicles.car,
    bike: data.vehicles.bike
}
pm.variables.set("vehicles",vehicles);

I'm getting data from external data file and its as following

[
   {
      "Vehicles":[
         {
            "car":"BMW",
            "bike":"YAMAHA"
         }
      ]
   }
]

I have run it through collection runner. and got the request body as vehicles:[object object] The data is not passing

Upvotes: 0

Views: 4234

Answers (1)

Shababul Alam
Shababul Alam

Reputation: 131

Here objects are coverted into a string. Due to this the string returned [object,object].Assuming you are using a JSON file for external source you can use JSON.stringify() to solve this. Change the following part of your code:

let car, bike;
var vehicles = {
    car: data.vehicles.car.stringify(),
    bike: data.vehicles.bike.stringify()
}

Upvotes: 4

Related Questions