ThomasReggi
ThomasReggi

Reputation: 59605

Use response object from one request in body of another [postman]

In one request I have the following:

var data = JSON.parse(responseBody)
postman.setGlobalVariable("myData", data.myData)

I'm looking to include this myData in the request body for another request.

The body for that request looks something like this:

{
  "myData": "{{myData}}"
}

However this does not work. The global is set to [object Object] and that string gets sent to the server in the request body instead of the actual object.

Is there a way to attach this globals[myData] object to the request body in Pre-request Script? Something like the following?

requestBody.myData = globals[myData]

Upvotes: 0

Views: 3083

Answers (1)

teubanks
teubanks

Reputation: 710

Two things should fix this for you:

  1. remove the quotes around the {{myData}} variable in your post body
  2. stringify the myData variable

It should look like this in your test:

postman.setGlobalVariable("myData", JSON.stringify(data.myData))

And this in your body

{
  "myData": {{myData}}
}

Upvotes: 2

Related Questions