How to export/download Response Body into an external file from Postman Collection Runner Results?
Asked Answered
A

7

17

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?

Arbil answered 5/1, 2018 at 12:12 Comment(1)
#70594141Twinned
D
16

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.

Dopp answered 5/1, 2018 at 12:30 Comment(2)
Hey Danny, i was thinking about your answer. How could it be possible to save all the different response_bodies from more than 1 iteration?. I want to store the "response_body" from 200 iterations and i want to use your methodHorror
I have seen postman collections that contained the response body for some requests, but not others, and was expecting this thread to explain why. In the meantime I am liking the, copy console output, option best to preserve responses.Surf
C
11

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.

Casement answered 26/6, 2018 at 18:41 Comment(0)
B
9

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!

Bunton answered 10/1, 2019 at 7:57 Comment(4)
Well it helped me! A+ you are a lifesaverJourneyman
Just created a gist with the template with suggested changed => gist.github.com/fmancardi/56844e80819830aae1fbfcc6376795c7Alvarado
Wil the solution help me in flask-python?Antimony
Hi I'm just saving the first request in my generated html even though I'm running more than 1 request, any idea on why is that happening? my command looks like this: newman run latina.postman_collection.json -d data.json -r cli,html --reporter-html-template template.hbsReni
C
2

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.

Commination answered 14/1, 2018 at 0:29 Comment(0)
O
2

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.

Octarchy answered 26/4, 2018 at 7:3 Comment(0)
A
2

Another way to do this is to copy the console output. This is not a programmatic solution but it is easy to do.

First open the Postman Console (click on the small icon at the bottom left).

enter image description here

Then click "Clear" at the top right corner of the Console. This will clear out existing console data from prior requests. It's best if this is cleared out so that when you copy result data it will include only the newly generated requests.

Next process your requests. Each request should appear as a new line in the console.

Then click the copy icon.

All of the request data, including responses, will be saved on your clipboard. Response lines all start with "Response Body:". You can then parse the text in Excel or elsewhere.

enter image description here

Absorbing answered 6/10, 2023 at 1:34 Comment(1)
I found this to be the easiest solution for me. I used the 'console.info' command and then set the 'Custom' filter to 'info' to only show the logs I want. This makes it much easier to copy only the results I want. Example code; console.info(pm.response.json()[0]["analysis"].dpv_match_code);Effendi
S
0

To export the response, Do follow these steps, As there is no simple method to do so.

  1. Add these lines in your test script pm.test(pm.response.value, true);
  2. Run the collection and export the collection
  3. Now you can have the response in an excel sheet

Note Disabled all tests if you want a response only. Modified the response in an excel sheet and remove the PASS text.

Seascape answered 9/11, 2022 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.