最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read stream responses generated by pm api sendRequest - Stack Overflow

programmeradmin2浏览0评论

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

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
Share Improve this question asked Nov 9, 2017 at 5:39 UlyssesUlysses 6,01510 gold badges55 silver badges92 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

Just use:

res.json()

This gives the response body in json format.

Usage:

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

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.

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);

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.

发布评论

评论列表(0)

  1. 暂无评论