Niati Arora
Niati Arora

Reputation: 129

Postman giving error for pre-request script

Using below snippet:

var timestamp = $.now().toString();
postman.setEnvironmentVariable("timestamp", timestamp);
postman.setEnvironmentVariable("apikey", obfuscateApiKey('yourapikey', timestamp));

Getting error: There was an error in evaluating the Pre-request Script: ReferenceError: $ is not defined

Not good in JavaScript, need to check.

Upvotes: 4

Views: 8079

Answers (3)

Danny Dainton
Danny Dainton

Reputation: 25921

You could achieve this without the need to use CheerioJS in Postman.

You could use the built-in {{$timestamp}} global variable that gets created at runtime but that only seems to work in URL, Headers etc.

So you could just do this, for example:

var timestamp = (new Date).getTime().toString()
postman.setEnvironmentVariable("apikey", obfuscateApiKey('yourapikey', 
timestamp))

Upvotes: 1

Gurpreet sandhu
Gurpreet sandhu

Reputation: 131

try

var timestamp = (new Date).getTime().toString(); instead of var timestamp = $.now().toString();

Upvotes: 3

Matt
Matt

Reputation: 13399

Postman doesn't use jQuery, but you can use a cutdown version of it called CheerioJS, see the documentation here.

It doesn't support now() but from the jQuery documentation for now():

The $.now() method is a shorthand for the number returned by the expression (new Date).getTime()

So you can just use the standard javascript here.

See this postman blog post for more info on using CheerioJS within postman.

Upvotes: 1

Related Questions