0
0
Postmantesting~5 mins

Saving responses in Postman

Choose your learning style9 modes available
Introduction

Saving responses helps you keep a record of what your API sends back. This makes it easy to check results later or share them with your team.

You want to compare current API results with past results to find changes.
You need to share API responses with teammates for debugging or review.
You want to keep examples of responses for documentation.
You want to test how your app handles different API responses by saving and reusing them.
You want to save responses to use in automated tests later.
Syntax
Postman
pm.test('Save response', function () {
    let responseBody = pm.response.text();
    pm.environment.set('savedResponse', responseBody);
});

pm.response.text() gets the full response as text.

pm.environment.set() saves data in Postman environment variables for reuse.

Examples
This saves the JSON response as a string in an environment variable called userData.
Postman
pm.test('Save JSON response', function () {
    let jsonData = pm.response.json();
    pm.environment.set('userData', JSON.stringify(jsonData));
});
This saves only the id field from the JSON response.
Postman
pm.test('Save specific field', function () {
    let jsonData = pm.response.json();
    pm.environment.set('userId', jsonData.id);
});
This saves the Content-Type header value from the response.
Postman
pm.test('Save response header', function () {
    let contentType = pm.response.headers.get('Content-Type');
    pm.environment.set('contentType', contentType);
});
Sample Program

This test saves the entire response body into an environment variable named fullResponse. It also checks that the response is not empty.

Postman
pm.test('Save full response body', function () {
    let responseBody = pm.response.text();
    pm.environment.set('fullResponse', responseBody);
    pm.expect(responseBody).to.not.be.empty;
});
OutputSuccess
Important Notes

Saved responses are stored as strings. Use JSON.parse() to convert back to objects if needed.

Environment variables are shared across requests in the same environment. Use collection or global variables if needed.

Remember to clear saved variables if they are no longer needed to avoid confusion.

Summary

Saving responses helps keep track of API outputs for later use.

You can save full responses or specific parts like fields or headers.

Use Postman scripts with pm.environment.set() to save data during tests.