azaveri7
azaveri7

Reputation: 929

How to send the output of a GET REST request as body of POST request using Postman

For our application, we are supposed to write the test scripts which test our REST API.

So I have written individual tests in postman in a collection. When ran individually, all tests pass.

Now to run all tests sequentially in a collection, I want the output of the first GET request (the response is an array of JSON objects), to pass as input body to next POST request.

The following code is written as a test in Tests of GET request.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("data", jsonData);

postman.setNextRequest("POST request");

I have named my post request as "POST request". But this test is failing.

Please let me know if I have done any mistakes.

Upvotes: 1

Views: 4258

Answers (2)

Vyasaraj
Vyasaraj

Reputation: 23

You're trying to set the entire JSON as an environment variable instead of property value. Try to change your line like this.

const jsonData = pm.response.json(); 
pm.environment.set('data', jsonData);

Later, use that environment variable as input to your POST request as {{data}}

Considering your JSON response is something like this.

  {
     "status": "SUCCESS",
     "message": null,
     "error": null,
     "id": 1234
 }

Hope I answered your question. I have recently written a blog on automating API's using postman you can refer here API Automation using postman

Let us know if it solves your problem.

Ah!! Missed this point.

If you want to send the entire JSON as input then use JSON.stringify()

Similar question asked

Upvotes: 2

Will
Will

Reputation: 320

The data should be set as part of the request for the POST operation.

Within Postman, in the body section for the POST request, you can set the data onto the request, either as a form field value or as the entire request body i.e. raw e.g.

{{data}}

Some additional information and examples are here: https://www.getpostman.com/docs/postman/environments_and_globals/variables

Upvotes: 1

Related Questions