Complete Guide: Programmatically Save Postman JSON Requests and Responses

Postman Saving JSON Requests and Responses

In REST API development and testing, exporting Postman’s requests and responses manually can be tedious. You have to manually extract the raw requests and responses, put them in a file, format, then save them. And you have to do this for all the test requests you have in your Postman collection. At times like this, you wish that there’s a way to programmatically save Postman JSON requests and responses on each execution.

Worry no more! This article will try to provide you with a way how you can automatically export each request and response from your Postman Collections. You even don’t have to be a developer or code-savvy to learn how to do it. But, it may require a little knowledge around Postman and JavaScript – just a little.

Key Pointers

  • Postman provides the raw requests and responses. Having said that, you can still manually extract the results from each executions.
  • You can programmatically save Postman JSON requests and responses with the use of Node.JS and JavaScript.
  • The article’s focus is REST in JSON format. Although there’s a way to make it work for XML formats, we will not cover them here.
  • In this article, we will be using a Windows environment – in our case it is Windows 10.

In this article, we will be using a JSON server from JSONPlaceholder that offers free fake APIs for our development and testing. We will use the HTTP GET method in showing how you can extract the Postman JSON requests and responses programmatically.

Let’s get started.

What Do We Need To Start Automatically Extract Postman Requests and Responses?

Before we go deeper, you need to make sure you have the needed tools before we can programmatically save Postman JSON requests and responses. Here’s what you need.

  • Windows environment, i.e. Windows 8, Windows 10, Windows 11.
  • Latest Postman native app. You can download it here.
  • Download and install node.js (download the latest LTS version here).
  • Windows command prompt shell or Windows Powershell. In this article, we will be using Windows Powershell but the steps should work also in command prompt or cmd.

Once you have everything installed in your machine, then we can start.

How To Check If Node.JS Is Installed And npm Is Working

Node.JS will play a very important role to enable us to extract the requests and responses from Postman automatically. So, we need to make sure that Node.JS was installed successfully and npm is working.

  1. Use Windows Search or press Window+X to launch Windows Powershell. It should not matter if you don’t use the Admin version.
  2. Type npm version and then press the Enter key.
NodeJS npm version In Windows Powershell

You should see something like above. In short, if you don’t see any errors after checking the NodeJS npm version you installed, then it means you are good to proceed. Otherwise, you will need to re-install or troubleshoot why it is not working.

Installation of npm Dependencies For Capturing The Postman Requests and Responses

The following steps below are to prepare the location from your Windows machine where you want to store the Postman’s requests and responses. In our example, which we also prefer, we will put it somewhere in our local drive D:\.

1. Create a new directory/folder where you want your requests and responses to be saved. You can either do it using Windows Explorer or command-line (mkdir postmanWriteToFile).

mkdir postmanWriteToFile

For this example, we will create the postmanWriteToFile folder in our drive D:\

Create a worskspace

2. Go to the new directory created.

cd postmanWriteToFile

Change directory to postmanWriteToFile

3. Then we will now start to download the npm library dependencies we will need. The first library we need is the express.

npm install express

npm install express

Notice that after you execute the command, a folder named node_modules was automatically created. This is where all the library modules or dependencies will be saved.

The file package.json will store the dependency packages information.

4. Next to install is fs.

npm install fs

5. Next, we will also need the body-parser.

npm install body-parser

6. Then, we will also need the shelljs.

npm install shelljs

7. Lastly, this is optional but since we will be working with JSON format, we will need the json-format.

npm install json-format

npm installation of other libs

8. After we complete the installation of the libraries we need, with your favorite text editor, open the file package.json.

Open to edit the package json file

9. Inside the package.json, we will now insert a few lines of code which we will need when we start the nodeJS script that will programmatically save Postman JSON requests and responses.

{
"name": "node-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node JSONReqstRes.js",
"stop": "taskkill /f /im node.exe"
},

Added Triggers to package json file

10. Notice from the previous steps, along the start trigger line, with the command node we are calling a script “JSONReqstRes.js“. This file will contain the NodeJS script that will handle the saving of JSON requests and responses to your local machine. And that is what we will have to create now.

With your favorite text editors, like Notepad or Notepad++, create a new file. Then insert the following line of codes below.

DISCLAIMER: We are not claiming that we own the following source codes. What we have below is a modified version of the original open-source ResponseToFile-Postman done by sivcan from GitHub. All the credits should be given to the original author of the code.

const express = require('express')
const app = express()
const fs = require('fs')
const shell = require('shelljs')
const format = require('json-format')
 
// Modify the folder path in which responses need to be stored
const folderPath = './Responses/'
const defaultFileExtension = 'json' // Change the default file extension
const bodyParser = require('body-parser')
const path = require('path')
 
// Create the folder path in case it doesn't exist
shell.mkdir('-p', folderPath)
 
// Change the limits according to your response size
app.use(bodyParser.json({ limit: '50mb', extended: true }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
 
app.get('/', (req, res) => {
  const usage = `/initSession - start new session for writting RQs RSs to file
  /write - writes RQ RS of current postman request to file
`
  res.send(usage)
})
 
app.get('/initSession', (req, res) => {
  sessionInit()
  console.log('-------------------------------------')
  console.log('New Session started ' + sessionString)
  res.send('Session initialised')
})
 
app.post('/write', (req, res) => {
  counter++
  let extension = req.body.fileExtension || defaultFileExtension
  // create session folder if it does not exist
  shell.mkdir('-p', sessionDirPath)
  let baseName = zeroPrefixed(counter) + '_' + req.body.requestName
  let requestFilePath = `${path.join(sessionDirPath, baseName + '_RQ')}.${extension}`
  let responseFilePath = `${path.join(sessionDirPath, baseName + '_RS')}.${extension}`
  console.log(sessionDirPath, baseName)
 
  const hasRawFlag = !process.argv.find(i => i.toLocaleLowerCase().includes('--raw'))
  const options = hasRawFlag && { indentation: '    ', stripComments: true, collapseContent: true, lineSeparator: '\n' }
  const responseString = format(req.body.response)
   
  writeToFile(requestFilePath, req.body.request)
  writeToFile(responseFilePath, responseString)
  res.send('')
})
 
 
app.listen(3000, () => {
  console.log('JSONReqstRes App is listening.')
  console.log(`Data is being stored at location: ${path.join(process.cwd(), folderPath)}`)
})
 
const writeToFile = (path, text) => {
  fs.writeFile(path, text, (err) => {
    if (err) {
      console.log(err)
      return 'Error'
    } else {
      return 'Success'
    }
  })
}
 
// counter for requests
let counter
let sessionString
let sessionDirPath
const sessionInit = () => {
  counter = 0
  sessionString = 'Session_' + sessionTime()
  sessionDirPath = `${path.join(folderPath, sessionString)}`
}
 
const zeroPrefixed = (i) => {
  let iString = i.toString()
  if (i < 10) iString = '0' + iString
  return iString
}
 
const sessionTime = () => {
  let currentTime = new Date()
 
  let seconds = zeroPrefixed(currentTime.getSeconds())
  let minutes = zeroPrefixed(currentTime.getMinutes())
  let hour = zeroPrefixed(currentTime.getHours())
  let month = zeroPrefixed(currentTime.getMonth())
  let day = zeroPrefixed(zeroPrefixed(currentTime.getDate()))
  const year = (currentTime.getFullYear()).toString()
  return year + month + day + '_' + hour + '_' + minutes + '_' + seconds
}
sessionInit()

Then save it with the name JSONReqstRes.js. Save that new file inside the directory we created.

12. We are almost done! DO NOT CLOSE your Windows Powershell or command-line console just yet. We will need it later. In the meantime, you can just minimize the window.

Integrating Node.JS Script To Postman Collection

Since our Node.JS preparation is complete, we now need to integrate the changes we made to work with our Postman Collections.

And to do this, we will be implementing a couple of JavaScript codes and adding them to our Postman Collections. These codes will enable Postman to call the functions implemented in JSONReqstRes.js.

1. Open your Postman native application then go to your Collection Pre-request Script tab. Then we need to insert the following JavaScript codes. Save the changes after.

// ################################################################################
// ######### Saving RQs and RSs in a file utility                        ##########
// ################################################################################
   
postman.setGlobalVariable('utils', () => {
    const stringify = false
    let verbose = true
 
  /**
   * Function which is used in order to save the data in postmans global variable scope.
   * @param {String} varName name of the value to be saved as postmanGlobal
   * @param {String} varValue value of the named value, it will be saved as a string
   * @param {Boolean} verbose console log output for the user to see given name and value
   */
  const setVar = (varName, varValue, verbose) => {
    if (verbose) {
      console.log('setVar ' + varName + ': ' + varValue)
    }
    pm.variables.set(varName, varValue)
    pm.globals.set(varName, varValue)
  }
 
  /**
   * Getter function which retrieves item from postmans global scope.
   * @param {String} varName name of the variable to get
   */
  const getVar = (varName) => pm.variables.get(varName)
 
  /*
  * set verbose mode
  * @param {boolean} setVerbose
  */
  const setVerbose = (setVerbose) => {
    verbose = setVerbose
  }
 
  /**
   * Sends request and response to local server for writing to file
   */
  const writeToFile = () => {
    let writeToFile = getVar('writeToFile')
    if (!writeToFile) { return }
    let dataToFile = {
      // eslint-disable-next-line no-undef
      requestName: request.name,
      fileExtension: 'json', 
      request: 'HTTP Method: ' + pm.request.method + '\r\n' + pm.request.headers + '\r\n' + 'REST API: ' + pm.request.url.getPath() + '\r\n' + 'Request Parameters: ' + pm.request.url.getQueryString() + '\r\n\n' + 'Request Body: ' + '\r\n\n' + pm.request.body.raw,
      response: JSON.parse(pm.response.text())
    }
 
    pm.sendRequest({
      url: 'http://localhost:3000/write',
      method: 'POST',
      header: 'Content-Type:application/json',
      body: {
        mode: 'raw',
        raw: JSON.stringify(dataToFile)
      }
    })
  }
 
  /**
   * Starts session for writting RQs and RS to file
   */
  const writeToFileStart = () => {
    setVar('writeToFile', true)
 
    pm.sendRequest({
      url: 'http://localhost:3000/initSession',
      method: 'GET',
      header: 'Content-Type:application/json'
    })
  }
  /**
   * Stops writting RQs and RS to file
   */
  const writeToFileStop = () => {
    setVar('writeToFile', false)
  }
 
  return {
    json: {
      writeToFile,
      writeToFileStart,
      writeToFileStop
    }
  } 
}); 
Postman Collection Pre-request Script tab

Highlights from the code

The JavaScript code we added is a global utility that contains functions that will communicate and call the functions from the JSONReqstRes.js. Although there are 3 main functions in the code, we will only need 2 of them to extract the requests and responses.

  • writeToFileStart – This function will call the /initSession inside the JSONReqstRes.js, a signal that a new session is started.
  • writeToFile – This function will capture the necessary information needed from your RQ and RS and save them into a JSON parsed data file variable named dataToFile . Then the information from dataToFile will be used in sending an internal Postman request which will call the /write inside the JSONReqstRes.js.

2. Now that we have the global function in place, we need a way to call them when we are sending our requests.

First, we need to call the initialization function called writeToFileStart.

You can put this in the Pre-request Script tab of the very first request of your flow.

let utils = eval(globals.utils)()
utils.json.writeToFileStart();
Postman Pre-request Script

3. Next is to put the code below on all the requests’ Tests tab of your flow. This code will call the writeToFile function that is needed to capture and save the request and response of the test step you are executing.

let utils = eval(globals.utils)()
utils.json.writeToFile();
Postman Tests Tab

NOTE!!!

You need to add this code to all the request steps you have in your flow. Although, a quicker way to implement this is by adding the code in the Tests tab of your collection. In that way, the code will assume that all of the requests in your collection will need to store the requests and responses.

How Start Collecting Postman’s Requests and Responses

Once everything is set up and ready, we can now capture each request and response from your Postman collection.

1. As we’ve mentioned before, we asked for you to minimize the console (Windows Powershell or DOS Command Prompt) where we created the directory that will store all the requests and responses, i.e. postmanWriteToFile.

Type and enter npm install  or just npm i. This will check if there are dependencies that need to be updated or installed.

2. Then if there are no errors, type and execute the command npm start.

This command will trigger the start script, which is the same as doing the command node JSONReqstRes.js .

Notice the line from the console where all responses will be saved. The “Responses” folder will be created automatically by the script if it does not exist.

Save To Responses Folder

3. Start running your Postman collection.

For each run, a new session folder is created. Then for each request invoked, a pair of JSON files will be created containing the request sent and the response received.

Save Postman JSON Requests and Responses

4. To stop logging the requests and responses, from your console, press CRTL+C simultaneously. This will terminate logging the requests and responses from your Postman.

5. As an additional step, to make sure that no node.exe process is running anymore in the background, type and enter the command npm stop.

Stopping npm

Let’s Wrap Up!

We are done!

Just some notes about the implementation above which we would like to share.

If configured correctly, all Postman’s requests and responses are saved in the Responses directory (inside where you created the postmanWriteToFile folder). And they are separated by folder with the format Session_YYYYMMDD_HH_MM_SS.

And on each Session folder, you will see all the saved/stored RQ and RS by pair and prefixed by a number.

For each request, besides the raw request sent from each Postman test step, we added some JavaScript to allow readers to see what are the method, parameters, and other variables used upon sending the request. This is helpful especially for the HTTP GET method where there is no content in the body of the request.

The additional information added on each request is as follows:

  • HTTP method
  • Headers
  • REST API used
  • Request parameters (helpful for GET methods)
  • Request Body (for GET, this part will be undefined)

If the request is not using the GET method, here’s an example of what you will see in the log. Normally, for the likes of POST methods, there are no parameters (unless required by the schema or API). That is why you will see that the “Request Parameters” contain nothing.