12
votes

I am working on a project where I have to hit a web service multiple times with different values of a variable

For example, http://mywebservice.com?variable1={{value}}

and different values are passed using Postman collection runner.

I want to download the response body of all the requests into a file. How do I do that?

5

5 Answers

12
votes

I don't think you can download the response body of the request in the collection runner - You can export the test results but i'm not sure that this contain the response body data. You can also click on each request in the runner and see the response body but this is still all within the UI.

For individual requests you can use the Send and Download option, which will show you the response body:

enter image description here

I'm sure there is some workaround that you could do to save the response body as an environment or global variable and then export this after but it seems like an awkward and slightly hacks solution.

pm.globals.set('response_body', JSON.stringify(pm.response.json()))

This would get overwritten each time though, so you would need to change the variable name each time, or have something in your code to change its name, to get a unique set of them.

These can then be exported and saved locally using the Manage Environment section.

9
votes

Here is a simple workaround if you're OK with storing the final data in an Environment Variable & just copying it to a .JSON file in a text editor after Collection Runner completes.

First you'll need to create an environment (great tutorial in this blog post on Postman), and define a variable called responseData, with the value [].

Then, add the following code under 'Tests' in Builder & save your collection. Postman environment variables are intended to be used as string, so we will parse the object then push the JSON responses from the API into the object array.

var jsonData = JSON.parse(responseBody);
var old = pm.environment.get("responseData");
old = JSON.parse(old);
// filter jsonData if needed

old.push(jsonData);
old = JSON.stringify(old);
pm.environment.set("responseData", old);
console.log(pm.environment.get("responseData"));

Now you can retrieve a nested JSON object with all the response data included by viewing the environment variables value (see example below).

Warning: you must reset the responseData value as [] after every use of Collection Runner in order to avoid keeping data from previous runs.

8
votes

I faced this situation and solved it by using the CLI tool newman

First you need to export your collection and the environment as JSON files. Then install newman using the command:

sudo npm install -g newman

Then if you want a neat looking HTML report for the results, then first install the external reported newman-reporter-html with the below command

sudo npm install -g newman-reporter-html

You can generate the report now by running the following command:

newman run <path to your collection json file> -e <path to your environment json file> -r cli,html

By default, the HTML file will not contain the request and response body. In order to render that, first download the default handlebars template and then tweak it a little bit. You can find the default handlebars template here. Download the file and save it as template.hbs. Then open it in any editor and look for the code where it is rendering the Status Code. It might look like this:

<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Status code</div><div class="col-md-8">{{response.code}}</div><br/>

Below this part, add the following lines:

<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Request body</div>
<div class="col-md-8">
    <textarea class="json" disabled rows="8" cols="70">
        {{request.body}}
    </textarea>
</div><br/>
<div class="col-md-12">&nbsp;</div>
<br/><div class="col-md-4">Response body</div>
<div class="col-md-8">
    <textarea class="json" disabled rows="8" cols="70">
        {{response.body}}
    </textarea>
</div><br/>

Now you can run the following command to render the HTML with request and response body:

newman run <path to your collection json file> -e <path to your environment json file> -r cli,html --reporter-html-template template.hbs

Hope this helps!

2
votes

By running a local server, and then using scripts in Postman to build a request to send to that server, you can write to your file system.

Here's a blog post about how to do that using the collection runner. You can also do the same thing using Newman.

For your project, you can store the response body in a variable, and then pass that variable as the payload in a POST request to your local server. Your local server will be listening for POST requests, and will write the data to your file system.

2
votes

Taking hint from here - http://blog.getpostman.com/2017/09/01/write-to-your-local-file-system-using-a-postman-collection/, below is a nodeJS server I wrote which will capture the requests and responses and print them one by one along with request name(which you have set in Postman) and URL.

var fs = require('fs'),
newman = require('newman'),
allRequest =[],
allResponse = [],
allName = [],
requestUrl = "",
allRequestUrl = [];


newman.run({
collection: '//your_collection_name.json',
iterationData: 'your_iteration_file', 
iterationCount : 3
})
.on('request', function (err, args) {
if (!err) {

    //console.log(args);    // --> args contain ALL the data newman provides to this script.
    var requestBody = args.request.body, 
        request = requestBody.toString(); 

    allRequest.push(JSON.parse(request)); 
    var responseBody = args.response.stream,
        response = responseBody.toString();
    allResponse.push(JSON.parse(response));

    var nameBody = args.item.name;
    allName.push(nameBody);
    var protocol = args.request.url.protocol;
    var host = args.request.url.host;
    var path = args.request.url.path;
    requestUrl+=protocol+"://";
    for(var j = 0;j<host.length;j++)
    {
        requestUrl+= host[j];
        if(j!=host.length-1)
        {
        requestUrl+=".";
        }
    }
    requestUrl+='/';

    for (var k =0;k<path.length;k++)
    {
        requestUrl+= path[k];
        if(k!=path.length-1)
        {
        requestUrl+= "/";
        }
    }
    allRequestUrl.push(requestUrl);
}
 })
 .on('done', function (err, summary) {
fs.writeFile('test.html',"");
//modify html output here.
for(var i =0;i<allRequestUrl.length;i++)
{
    fs.appendFileSync('test.html', "<br><h>Name:  </h>");
    fs.appendFileSync('test.html',allName[i]);
    fs.appendFileSync('test.html', "<br><h>URL:  </h>");
    fs.appendFileSync('test.html',"\"" + allRequestUrl[i] + "\"");
    fs.appendFileSync('test.html', "<br><h>Request</h><br>");
    fs.appendFileSync('test.html',JSON.stringify(allRequest[i],null,4));
    fs.appendFileSync('test.html', "<br><h>Response</h><br>");
    fs.appendFileSync('test.html',JSON.stringify(allResponse[i],null,5));
//fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
}
});

To run the above code, you need to install newman which is Postman's CLI. First of all install node and npm in your computer, then go to your directory and install newman via -

    npm install newman

Then copy paste the above code in a js file 'filename.js' and run it by below command -

    node filename.js

The output containing the information you require will be saved in a file named "test.html" in the same directory.