erik37
erik37

Reputation: 41

Postman - Using complete response from one request as body of another request

I have a request were the exact response from one request is the body of another request.

Is there an easy way to store the response from one request to reuse as the body of another request in Postman?

I tried storing the response body in a global variable customerData and then having the body of my other request be {{customerData}} but this does not work.

Thanks for the help.

Upvotes: 4

Views: 5757

Answers (2)

Anant Trigune
Anant Trigune

Reputation: 171

API-1 - Test Tab

//Get the response of first API

var jsonData=JSON.parse(responseBody);

//Convert the JSON response to String

var jsonString=JSON.stringify(jsonData);

//Store in environment variable

pm.environment.set("MyPayLoad", jsonString);



API-2 - Body    
{{MyPayLoad}}

This way response from API-1 is passed as payload to API-2

Upvotes: 3

Danny Dainton
Danny Dainton

Reputation: 25901

You could achieve this by using the sendRequest() function in the Tests tab of your first GET request. This will send the request and get the data once this has completed, it will then POST that same response data to another endpoint.

This is a very basic example that can be added to the Tests tab, this can be changed/adapted to your own context:

pm.sendRequest({
    url: 'localhost:3000/post',
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(pm.response.json())
    }
}, (err, res) => {
    console.log(res)
})

This is what it looks like in Postman - I've sent a basic request to the /get route and in the Tests tab, I'm using that response data as the payload for the POST request by inserting the pm.response.json(). You can see the request body for the /post route has been taken from the first request.

Postman

Upvotes: 1

Related Questions