最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Postman giving error for pre-request script - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Dec 15, 2017 at 15:55 Stefan 2,0682 gold badges36 silver badges55 bronze badges asked Dec 15, 2017 at 15:15 Niati AroraNiati Arora 1292 gold badges3 silver badges8 bronze badges 2
  • Sounds like you need to include jquery. Also, not sure this has anything to do with java... – user184994 Commented Dec 15, 2017 at 15:17
  • Do you mean Postman, the API testing app? What do you do exactly? Seems to me that you're just missing a lib like jQuery that defines the $ – Stefan Commented Dec 15, 2017 at 15:18
Add a ment  | 

3 Answers 3

Reset to default 3

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))
发布评论

评论列表(0)

  1. 暂无评论