Collection Runner- How we can save response in local computer for documentation

As we know when user run individual request from workspace directly, User can save response in text file through ā€œsave to a fileā€ from ā€œSave Responseā€ dropdown.

But When we run request(s) from collection runner, How we can save the response?

I can see 2 options which are not feasible as per my opinion.
A. Open the collection runner, Select the required collection and request, select the ā€œsave responsesā€ checkbox and run it.
click on individual request, select the response body and manually copy the response and paste it to locally which is not feasibly because we canā€™t do manually one by one for 100 requests.

B. Click on ā€œexport Resultā€ button in the Run Result tab where results are showing but saved file contain the status(pass/fail) rather then response.

P.S. To save the response in text file locally just for the future response, Looking without newman.
Let me me required more details.

1 Like

Hi @aakashjain8693,

By locally do you mean that you are running Postman remotely?

The only thing I can think of to achieve this, without using newman, is to save the request and response as environment variables, to which then you can export that environment and get the values out from there. Aside from that you can also send the response to a special / custom web server for recording purposes, using pm.sendRequest and putting the response in the request payload.

At the moment, I cant think of any other ideas on how to achieve this. Hope one of the options above are viable!

Best,
Orest

2 Likes

Thanks!! Since we are in testing world, Always history and result important.

cc @allenheltondev

I agree with @odanylewycz. To make this easy, you can add a test script on your collection so it automatically runs for every request.

let responses = pm.collectionVariables.has('collectionResponses') ? pm.collectionVariables.get('collectionResponses') : [];

responses.push(pm.response.json());

pm.collectionVariables.set('collectionResponses', JSON.stringify(responses));

When the collection is has completed, you will have every response from every request in that collection run in the collectionResponses collection variable.

2 Likes

I have the same problem. I want to run many get requests at with the runner but canā€™t save the response body from each in a file at the end.

I tried your code to write it in a global variable but in the new postman version there is no such a function:
ā€œThere was an error in evaluating the test script: TypeError: responses.push is not a functionā€

1 Like

I had the same problem and found that Postman saves variables as strings. You need to JSON.parse() the variable to get it back into an array so that you can push into it.

let responses = pm.globals.has('responses') ?  JSON.parse(pm.globals.get('responses')) : [];
responses.push(pm.response.json());
pm.globals.set('responses', JSON.stringify(responses));
1 Like

@allenheltondev thanks for the tip and @jannieo9
Sorry new to this, but once all the variables are stored in the variable, how can i export that? and any way to save that as a csv? (without newman).

Thanks!

so got this far to access the response value
https://postman-echo.com/get?var={{responses}}

But any tips on preformatting that response to be in csv format? maybe from the test script part?

Hello @allenheltondev, I am new to Postman, scripting, and collection variables (and forum participation in general so I am a certified newbie!!). In the test script you provided, is that attached to the body of the get request and is it coded in javascript? Iā€™m not sure how to make use of it in Postman/Collection Runner. I am looking at documentation and trying to figure it out, however if you could give me some pointers that would be great!

Hope this thread is still active and hope it is appropriate question for this thread.

Thank you for your time!

Regards.

Hey there!

Firstly, welcome to the Postman community and congrats on your first post! :confetti_ball:

Test scripts can be a part of specific requests, folders and also, in this situation, a collection itself. You can read more about using them here.

For the test script provided by Allen, you would attach that to your collection by clicking on the collection item and then under the ā€œTestsā€ tab. These tests are written in Javascript and using ChaiJS BDD syntax.
The collection runner will then run all tests attached to requests, folders and collections that you set to run in the run order. :smile:

Kevin, thank you for your response! Thereā€™s just so much info to go through you just saved me probably hours of searching. I browsed through the link you gave me and it looks like exactly the area of help that I need.

When I added a test as you instructed using Allenā€™s script it gave me the error: ā€œresponses.push is not a functionā€. Iā€™ll go through the link and see if I can figure it out. Thanks again!!!

1 Like

Hey @Tawfiqster , welcome!

The error you received means that the responses object is not an array. You can modify the script I wrote like this:

let responses = pm.collectionVariables.get('collectionResponses')
if(responses) {
  responses = JSON.parse(responses);
} else {
  responses = []
}

responses.push(pm.response.json());
pm.collectionVariables.set('collectionResponses', JSON.stringify(responses));

The difference here is parsing the value from the collectionResponses environment variable instead of using it as is. Likely what was happening was the variable content was a string instead of an array (which very much is what it was since weā€™re using JSON.stringify to set the value in the first place!)

1 Like

Thank you Allen! I actually found another response you had posted where you modified for Pranavdavar and I was able to put that in without errors. Still figuring a lot of things out but slowly getting there thanks to the community support. Much appreciated!

Regards.

1 Like

Let us know if you need any more help!

@allenheltondev How would you go about saving the original request with the response (from above code). Thank you!
Used this, but the URL is on a separate line
let responses = pm.collectionVariables.get(ā€˜collectionResponsesā€™)

if(responses) {
responses = JSON.parse(responses);

} else {
responses =
}

responses.push(pm.response.json());
responses.push(pm.environment.get(ā€˜urlā€™) + pm.request.url);

pm.collectionVariables.set(ā€˜collectionResponsesā€™, JSON.stringify(responses));

Welcome to the community @megvster :wave:

If I understand your question correctly, youā€™d like to save the url of the request along with the response?

To do that, you could make an object and save that to the environment variable.

let resposnes = pm.collectionVariables.get('collectionResponses');
if(responses) {
  respones = JSON.parse(responses);
} else {
  responses = [];
}

responses.push( {
  url: `${pm.environment.get('url')}${pm.request.url}`,
  response: pm.response.json()
});

pm.collectionVariables.set('collectionResponses', JSON.stringify(responses));

Iā€™m not sure what your url environment variable is, but thatā€™s how I would do it. :slight_smile:

Guys, if you are like me a Postman beginner, I comment on the response of Allen:

First learn how to run a batch of commands:https://stackoverflow.com/questions/56448021/bulk-post-put-api-requests-using-postman-or-any-other-means

  • modify the request you wan to execute
  • add a test and copy/paste the code of Allen
  • then execute the batch
  • then at the end of the execution,
  • back to the collection end edit. Click on Variables. One variable named collectionResponses has all the results and it is cool (thanks Allen)! Copy the content of collectionResponses into the clipboard and save into a file.
  • The next problem is that the responses are in a file of one long line!
    To split the line you can make usage of the sed command.
    Example:
    cat results.json | sed ā€˜s/}}}/\n/gā€™ > result_f.txt
    the string ā€˜}}}ā€™ is replaced by ā€˜\nā€™, end of line.

This procedure is not easyā€¦

  • Code to add to the test
  • Execute the test
  • Extracting the result from the collection
  • Copy/paste
  • Saving into a file
  • Split the file
    I think Postman as a product lacks this func. My opinion.

But thanks 1000x Allen. You saved my journey!

2 Likes

Iā€™m happy I could help!

1 Like

The answer I posted on stackoverflow:
Bulk POST/PUT API requests using POSTMAN or any other means (stackoverflow)

Hi @allenheltondev ! When I use your script as a collection test and I try to run the collection, I get the following error in the console:

ā€œJSONError: Unexpected token ā€˜<ā€™ at 1:1
200Successful</Status
^ā€

My requests are in XML and I assume the error is related to this?

Any advice would be much appreciated!