How to Write Global Functions in Postman

前端 未结 8 1763
借酒劲吻你
借酒劲吻你 2020-11-30 04:59

I need help writing a common function to use across a collection of requests which will help with building a framework.

I have tried using the below format

8条回答
  •  醉话见心
    2020-11-30 05:13

    The problem had perplexed me for a while until I found the common way mentioned above. However, it still leaves a warning icon for each eval line, which indicates “eval can be harmful” in the postman interface. Recently, I’ve found another way and post it here: Users can create a prototype object with the proper function you want in the pre-request script section, like this:

    Object.prototype.sayHello = function(name){
    console.log(`Hello! ${name}`);
    };
    

    and call that function everywhere after that. It just required a defined object, like this:

    let obj = {};
    obj.sayHello(‘Griffin’);
    

    Or you don’t even need the declaration of the object but use some built-in objects instead, like lodash (you pretend it has the function :smile: )

    _.sayHello(‘Griffin’);
    

    It’s working on my side. I also posted it in postman forum here https://community.postman.com/t/global-functions-via-collection-level-folder/5927/6?u=franksunnn110

提交回复
热议问题