简体   繁体   中英

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.

try

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

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM