how to get whole html or json repsonse of an URL using Newman API

守給你的承諾、 提交于 2019-12-10 22:13:43

问题


Whenever I run following from command line

newman run https://www.getpostman.com/collections/abcd1234

I get output displaying the statistics of failed and execute.

But I am looking for the complete HTML or JSON response from the URL to be printed on terminal after executing the above Newman query.How can I achieve this?


回答1:


You have to add some log output in your requests.

For the requests where you want to see the response output add the following in the Postman Tests tab:

console.log(responseBody); // full response body

If you want to log a specific part you have to parse the response body into a JSON object:

let response = JSON.parse(responseBody);
console.log(reponse.myprop); // part of the full response body

Now if you run this collection with newman the CLI reporter will print the console log parts as well.




回答2:


You need to use Postman API.

So you need to run something like this

newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey

(see http://blog.getpostman.com/2018/06/21/newman-run-and-test-your-collections-from-the-command-line/) You can get ApiKey in your Postman Cloud. You need to go to the workspace -> Integrations -> Browse Integrations -> Postman API View details -> Detail Get API Key/Existing API Keys

If you also need to add environment (if you use Variables), what you need is to run the same command with -e parameter 'newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e dev_environment.json'

But what if you have your environment in the cloud as well? According to this document https://www.getpostman.com/docs/v6/postman/collection_runs/command_line_integration_with_newman you can pass URL as value. So you may run something like this

newman run https://api.getpostman.com/collections/myPostmanCollectionUid?apikey=myPostmanApiKey -e environments/{{environment_uid}}?apikey=myPostmanApiKey

It worked for me, hope this will help




回答3:


I am using newman for webservices and microservices testing. This works fine for me.

summary.run.executions[0].response.text().toString()

After done event you should be able to get the response.

d is the collection exported from Postman.

newman.run({
            collection: d,
            // reporters: 'cli',
            iterationCount: 1,
            timeoutRequest: 10000,
            timeoutScript: 5000,
            delayRequest: 0,
            insecure: false, 
        }).on('done', (err, summary) => {
            if (err || summary.error) {
                console.error('\ncollection run encountered an error.');
                reject(summary.error);
            }
            else {
                var xml = summary.run.executions[0].response.text().toString();
                console.log(xml)
            }
        })
    })


来源:https://stackoverflow.com/questions/42543935/how-to-get-whole-html-or-json-repsonse-of-an-url-using-newman-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!