postmanonclicklistenerpostman-testcasepostman-native-app

Save custom response in json file in postman


In postman, I am getting response from 3rd party API and the response contains unnecessary data.

I want to get few keys from the resposne and save in json file when I click on SAVE RESPONSE button inside postman.

 "goods": {
        "193651": {
            "goodID": "193651",
            "name": "Erexper",
            "quantity": "1",
            "price": "11900",
            "categoryID": "0",
            "categoryName": "Без категории",
            "purchasingPrice": "2500"
        },
        "193653": {
            "goodID": "193653",
            "name": "Immunomin",
            "quantity": "1",
            "price": "11900",
            "categoryID": "0",
            "categoryName": "Без категории",
            "purchasingPrice": "2500"
        },
     
        "193656": {
            "goodID": "193656",
            "name": "Life Factor",
            "quantity": "1",
            "price": "11900",
            "categoryID": "0",
            "categoryName": "Без категории",
            "purchasingPrice": "2500"
        }
    }

From this response, I just want to get all the keys inside goods e.g.

{ 193651, 193653, 193656 }

Note: I don't want to print in the console and in visualizer section.

I just want to parse the response when SAVE RESPONSE button is clicked.

Or if it is possible to print modified response in the postman response body section.


Solution

  • In the tests tab you could do this

    Parsing the Json and getting only the keys you can use this. Reference to the post

    var json = pm.response.json();
    const onlyKeys = Object.keys(json["goods"]);
    console.log(onlyKeys);
    

    Postman says you need to use a visualizer for modifying the response and displaying it and the save response button is always going to save the response you received not the modified one.

    I guess you would need to create your own local server which accepts the modified json and saves it to a file, the postman documentation also gives a similar example.

    Then you would end up doing something like this.

    
    setTimeout(() => {
        pm.sendRequest({
            url: 'http://localhost:3000/write',
            method: 'POST',
            header: 'Content-Type:application/json',
            encoding: 'binary',
            body: {
                mode: 'raw',
                raw: onlyKeys
            }
        }, function (err, res) {
            console.log(res);
        });
    }, 5000);