# Outputting request + response pairs to file

**URL:** <https://community.postman.com/t/outputting-request-response-pairs-to-file/13531>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [June 19, 2020, 2:47pm UTC](https://community.postman.com/t/outputting-request-response-pairs-to-file/13531 "2020-06-19T14:47:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankdoylezw](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankdoylezw/32/7322_2.png) [@frankdoylezw](https://community.postman.com/u/frankdoylezw)\
**Post date:** [June 19, 2020, 2:47pm UTC](https://community.postman.com/t/outputting-request-response-pairs-to-file/13531/1 "2020-06-19T14:47:19Z")

</div>

Hi

I’m currently using this (very helpful!) to output responses to file:

> **[Write Responses To File](https://documenter.getpostman.com/view/3407886/RWgp1fB5?version=latest)**
>
> This collection makes it easy for users who want to write the response of each request to a file.
> This can be extended to write anything for eg. meta information or value of variables being used.
> 
> 
> This project is using a powerful feature built in...

Is there a way to write the requests to file, too? Ideally I’d like to have file output with the request & response pairs logically named.

Has anyone done this or can anyone suggest a solution, please?  
@singhsivcan - I believe you wrote the original server. Would it be possible to modify the script.js such that the requests are also outputted?

Thanks,

Frank

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [June 23, 2020, 10:57am UTC](https://community.postman.com/t/outputting-request-response-pairs-to-file/13531/2 "2020-06-23T10:57:14Z")

</div>

@frankdoylezw Try the following script by replacing your existing Tests script with the following one:  
_To edit the script: Right click the collection \> Edit \> Tests tab._

```javascript
function getPayload (response) {
    // Request Separator
    let body = '\n\n=======REQUEST DATA=======\n\n';

    // Attach the stringified request
    body += JSON.stringify(pm.request);

    // Response Separator
    body += '\n\n=======RESPONSE DATA=======\n\n';

    // Attach the response
    body += response;

    return body;
}

// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'writeFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: false,
    responseData: getPayload(pm.response.text())
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }
}, function (err, res) {
    console.log(res);
});

```

What I am doing here is appending the payload of the request also in the data going to the server along with the response.

---

<div class="post-metadata">

**Author:** ![frankdoylezw](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/frankdoylezw/32/7322_2.png) [@frankdoylezw](https://community.postman.com/u/frankdoylezw)\
**Post date:** [June 23, 2020, 2:39pm UTC](https://community.postman.com/t/outputting-request-response-pairs-to-file/13531/3 "2020-06-23T14:39:53Z")

</div>

@singhsivcan Thank you - that is exactly what I was hoping for! Your scripting skills are something to aspire to, for sure 🙂

I had only a single query, please: the counts of requests in the collection vs tests vs files written do not tie up, for some reason. I have 182 tests in 138 requests, but when I run that collection only 63 files are written? Am I missing something, please?

 ![2020-06-23_153815](https://us1.discourse-cdn.com/getpostman/original/3X/0/2/026869039588ae31a354279072cd95f5d9023531.jpeg)

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [June 25, 2020, 6:04am UTC](https://community.postman.com/t/outputting-request-response-pairs-to-file/13531/4 "2020-06-25T06:04:49Z")

</div>

@frankdoylezw you probably have multiple requests with the same name, and the files are being overwritten.

Please pull/clone the latest changes from here: [https://github.com/sivcan/ResponseToFile-Postman](https://github.com/sivcan/ResponseToFile-Postman)

Next, you can edit the `opts` in the Tests script to the following:

```auto
// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'json',
    mode: 'writeFile',
    uniqueIdentifier: true,
    responseData: getPayload(pm.response.text())
};

```

The `uniqueIdentifier` property makes sure that a random number is appended to every file name, thus everytime it’ll create a new file and write your request and response to it.

Let me know if it works for you 🙂
