8

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 4

5

Just use:

res.json()

This gives the response body in json format.

Usage:

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

4

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.

2
  • exactly same here. res.toJSON() didn't convert the stream into text. .text() worked for me.
    – Tosh
    Commented Sep 27, 2023 at 8:56
  • For my case, this is the correct answer since the data sent from the request is a simple string.
    – vchan
    Commented Nov 15, 2023 at 9:09
2

If response of below request is in XML format,

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

I am trying to convert response using below code

var jsonObject = xml2Json(res);

It is giving error saying

JSONError | Unexpected token u in JSON at position 0

When I used that same function with testscript, It is converting XML to hsonObject

var jsonObject = xml2Json(responseBody);
1
  • 3
    To get responseBody of xml response need to use below method var jsonObject = xml2Json(res.text())
    – Datta More
    Commented Mar 14, 2018 at 13:41
1

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.

1
  • @BabyGroot yeah, If you just need the response body, go for json() - indeed.
    – Alfageme
    Commented Nov 9, 2017 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.