How to read stream responses generated by pm api sendRequest

Viewed 5837

Trying to run a postman (pm) api call to run a request from the test tab. Getting back streams in response (logged in console as array containing integers).

Any idea as to how to read these streams.

Request:

pm.sendRequest({
    url: myUrl,
    method: 'GET',
    header: {
        'content-type': 'application/json'
    }
}, function (err, res) {
    console.log(res)
});

Response:

Object:{}
    code:200
    cookie:[]
    header:[]
        0:{}
        1:{}
        2:{}
        3:{}
        4:{}
        5:{}
        6:{}
    id:"e5d5d6d6"
    responseSize:55551
    responseTime:263
    status:"OK"
    stream:{}
        data:[]
            0:123
            1:10
            2:32
            3:32
            4:34
            5:115
            6:119
            7:97
            8:103
            9:103
            10:101
            11:114
            12:34
            13:32
            14:58
            15:32
            16:34
            17:50
            18:46
            19:48
            20:34
            21:44
            22:10
            23:32
            24:32
            25:34
4 Answers

Just use:

res.json()

This gives the response body in json format.

Usage:

pm.sendRequest('url', (err, res) => {console.log(res.json());}

You need to use toJSON() function on the Response object to serialize it to a human-readable format:

function (err, res) {
    console.log(res.toJSON())
});

See the pm Sandbox API for further reference.

This answer didn't work for me as my reply was a HTML page with embedded JS. I had to use pm.response.text() and painstakingly parse out the code I wanted by using .spit('\n') to get an array of lines, etc.

Related