How to Write Global Functions in Postman

前端 未结 8 1766
借酒劲吻你
借酒劲吻你 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:23

    If you want to call pm.sendRequest in a global function, try this:

    1. Define the global function in collection pre-request, like this:

      pm.globals.set('globalFunction', parameters => {
          console.log(parameters);
          pm.sendRequest('https://google.com/', function(err, resp) {
              pm.expect(err).to.not.be.ok;
          });
      });
      
    2. Use function like this:

      eval(globals.globalFunction)('hello world!!');

    Note that, I declared function using arrow style ()=>{}. Otherwise, it wouldn't work.

提交回复
热议问题