Hack To Add Your Own Snippets To Postman

Gopi Krishna
5 min readOct 19, 2019

--

Enthusiasm and laziness are the two things that drives development addicts.

Hack To Add Your Own Snippets To Postman

We usually consider laziness as something negative, but that’s not the case every where.

If you look at the history of communication between humans and computers (and technology in general), you will find how it’s been the lazy ones that have helped it improve the most.

From a time where one have to give commands to computers using punch cards, a process that will take hours on hours just to ask from the simplest tasks from a computer to a time where we can now talk to our computers, phones, cars, televisions… and they actually understand!(at least recognizes/processes 😉). And its my laziness 😩 & enthusiasm to write tests every-time made me to do this.

Enough of intro, let’s get on to how to add our own custom snippets to the Postman app in Windows Platform. The toughest part was to find a way to extract the contents of app.asar . 😓

To achieve this we use the 7zip. But alone with 7zip we cannot extract the app.asar contents, the secret sauce here is Asar7z plugin. Detailed instructions on how to add the Asar7z plugin to 7zip are within the plugin download page.

Stating,

To install the plugin into the 7-Zip installation folder, you need to create the “Formats” subfolder. After that, copy Asar.64.dll or Asar.32.dll (depending on your 7-Zip edition) to that sub-folder. If you do that, each time you launch 7-Zip, it will automatically find Asar7z and use it when opening .asar files.

File Location:

C:\Users\gopikrishna4595\AppData\Local\Postman\app-7.9.0\resources\app.asar

I observed that Postman maintains two versions of its application in the local system, make sure you take the copy of app.asar from latest version of app folder.

Two versions of Postman App

Take a Copy First before Extracting

We’re modifying a core file of Postman, so it’s important you have a way to revert back if anything goes wrong. Better make the changes on a copy of app.asar in another drive.

Now right click on the app.asar and extract contents to app/

Bazinga!

we successfully extracted the contentsof app.asar . Now edit this file 👇

D:\gopikrishna4595\app\js\vendor-shared.js

open the vendor-shared.js in your editor and search for this text 🔎

“name”:”Get an environment variable”

It’s a large file, you will find it at a line number some where like after 4,03,800

Hack To Add Your Own Snippets To Postman

Take a copy of one of the array objects and include one of our own functions, that we can easily use multiple times.

Existing Sample Object

{
// id
"id": "get-env-var",

// Test name displayed in Postman App
"name": "Get an environment variable",

// Description Of Test
"description": "Gets an environment variable",

// Snippet that will appear in Tests/Pre-request Script Tabs
"code": "pm.environment.get(\"variable_key\");",

// Scope of the Snippet added
"validity":["prerequest","test"]
}

Now that we know on how to write, I’m going to create few new snippets.

Sample Snippets

Snippet 1:var xml_req = xml2Json(request.data);
console.log(xml_req);
Snippet 2:var req = JSON.parse(request.data);
console.log(req);
Snippet 3:var res = pm.response.json();
console.log(res);

You can add your own code snippets instead, for each snippet make sure you name unique id, and suitable name and put the code snippet only at “code”:

Final Script

{“id”:”json-request-data”,”name”:”JSON Request Data”,”description”: “Parse JSON Request Body”,”code”:”var req = JSON.parse(request.data);\n console.log(req);”,”validity”:[“prerequest”,”test”]},{“id”:”json-response-body”,”name”:”JSON Response Body”,”description”: “Parse JSON Response Body”,”code”:”var res = pm.response.json(); \n console.log(res);”,”validity”:[“test”]},{“id”:”convert-request-xml-json”,”name”:”Request body: Convert XML body to a JSON Object”,”description”:”Convert request body from JSON to XML”,”code”:”var xml_req = xml2Json(request.data);\n console.log(xml_req); //Elemental Path Can be obtained from the developer console log”,”validity”:[“test”]}

Add it to the end of the array in the vendor-shared.js file and save.

Repack/Re-bundle

Now we need to repack/re-bundle all the extracted contents , Before moving on you should have Node Js, if not install Node Js in your system. Once done, install asar npm package globally.

Now navigate to the parent folder, in my case it is :

D:\gopikrishna4595
Hack To Add Your Own Snippets To Postman

open command prompt at this location, and repack the code into a bundle by executing

npx asar pack app app.asar

Hack To Add Your Own Snippets To Postman — Repacking the Code

Now copy the modified app.asar and replace it at

C:\Users\gopikrishna4595\AppData\Local\Postman\app-7.9.0\resources

Before replacing make sure you have a backup of original app.asar .

Now Launch the Postman application, open an existing request from a collection and check Tests tab for our Own customized Tests.

Et Voilà !

Et Voilà !🎉

We had successfully added our Own custom snippets to Postman.
By the way meet Ever “Enthusiast” Cooper 😄

By the way, Prior to this Manny Codes had written for iOS platform. Check it out here.

If you find it helpful 👍 or can be improved let know in comments 👇.

Please share it on Linkedin or twitter or other social media platforms. Thanks again for reading.

You can find me on twitter: @gopikrishna4595 and Linkedin (www.linkedin.com/in/gopikrishna4595)

--

--