Adding external libraries to postman

I have a customized node module for json validation, which i need to use it in my postman scripts but looks like postman only supports tv4. Does anyone have any idea what can be done in this case?

@rajireddy95 Have a look at this blog post http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/ and scroll down to TIP #5: reuse code.

This talks about how you can basically create your own JS Libraries and pull them into a variable and call them at any time.

Hope this helps.

I’m trying to do the same thing. Thanks for the response. Any chance you could give a quick example of how to import an external library (from npm) using this method?

@yoelspotts You’ll first have to check if the library in question has a CDN equivalent that you can use. For instance, if you’d like to use strman, you can head over to it’s CDN link: https://cdnjs.cloudflare.com/ajax/libs/strman/1.3.1/strman.js, copy the contents over into the value of an environment variable, say strman_code. Next, you can include this within your code by adding the following snippet of code as the first statement in every request level pre-request/test script:

const strman_code = pm.environment.get('strman_code'); // strman_code now contains the library code

(new Function(strman_code))();

This will allow you to use strman functions within your collection run.

While the above example is specific to one library, the usage can easily be extended to just about any library that has a CDN equivalent. :smile:

2 Likes

@kunagpal thanks so much!

Hi @kunagpal :smile: ,

i’m trying use the faker, so as you said checked for its CDN link: https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.js

i tried several times to copy the contents to environment variable faker_code. the postman app v6.1.4 got struck every time.

size of the faker.js content on the disk : 1.16MB

So i tried with this
https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js

i received an error stating

There was an error in evaluating the Pre-request Script: TypeError: Cannot set property ‘faker’ of undefined

can you help on this?

Thanks & regards,
Gopi :cowboy_hat_face: .

@gopikrishna4595 It looks like you’ve hit the limits of the data editor. This can be overcome by using pm.sendRequest as follows:

pm.sendRequest('https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js', (err, res) => {
  (new Function(res.text()))();
});

Hi @kunagpal,
Thanks for your time :grinning:.

still observing the below error in console,

ReferenceError | faker is not defined

TypeError | Cannot set property 'faker' of undefined

Following is the screenshot,

Following is the snippet i’m using in Pre-Request Script

pm.sendRequest('https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js', (err, res) => {
  (new Function(res.text()))();

var randomName = faker.name.findName();

console.log(randomName);

});

can you please tell, what and where i’m doing wrong ?

Thanks & regards,
Gopi :cowboy_hat_face: .

Greetings @kunagpal,

can you provide any more info on this ?

Thanks & regards,
Gopi :cowboy_hat_face: .

@gopikrishna4595 This is happening because the faker code from the CDN is trying to set the faker object as a property of window, which is undefined in the Postman Sandbox. You could work around this by explicitly defining window as an empty object before running the faker injection snippet.

Thanks @kunagpal :smiley:

i tried but unable to achieve.

not sure whether i did the same as you said, or not :confused:.

Can you help on how to achieve this ?

Update: sorry, i conveyed the wrong msg/way earlier.

Thanks & regards,
Gopi :cowboy_hat_face: .

Does this mean setting an environment variable with a key, value pair?
{ “strman_code”: “https://cdnjs.cloudflare.com/ajax/libs/strman/1.3.1/strman.js” }

Hi There,
Fairly new to postman, loving it so far, but bumped into a problem i’m struggling to work through. I need to set various time values using utc and timezones, utc through moment is fine, but setting timezone time i think i need moment-timezone lib. I’ve set this as a variable (tried local, environment and global) but still struggling. I think this is because moment-timezone needs moment before it can run, but i can’t seem to link them. I’ve got the below snippet in place but i get the following err
Moment Timezone requires Moment.js. See https://momentjs.com/timezone/docs/#/use-it/browser/
TypeError | Cannot read property ‘version’ of undefined
warnings-and-assert-as-expected: TypeError: Cannot read property ‘version’ of undefined

Code i’m running is (note i included eval as i tried a take on that too)


const momentTimezone = pm.variables.get('momentTimezone');
(new Function(momentTimezone))();
pm.variables.set('localeTime', momentTimezone.tz("America/Los_Angeles").format("DD/MM/YYYY HH:mm"));

var localeTime = pm.variables.get('localeTime');
console.log('Locale Time', localeTime);

Any help or advice to get this working would be great.

I also would like to add vetted libraries to “my personal sandbox”. Why is this made so hard and error prone? Why not have a feature that would allow the writer of the collection to add scripts to the sandbox?

Hi @ccapperea,

i experienced the similar.
if you are using moment.js in a single or less number of apis, instead of using the moment library by storing cdn url in the environment variables.

try import and use, like

var moment = require('moment');

pm.environment.set("expirationTime", moment(moment().add(30, 'days')).format("MM/DD/YYYY HH:mm:ss"));

How ever, i think importing the library multiple times is not a good practice with in a collection.

Thanks & regards,
@gopikrishna4595 :cowboy_hat_face:

Can Anyone suggest how to add aws-sdk library in pre-request script???

1 Like

Thanks, I’ll give this a go!
:+1: