How to Write Global Functions in Postman

前端 未结 8 1765
借酒劲吻你
借酒劲吻你 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条回答
  •  旧时难觅i
    2020-11-30 05:11

    You can declare a global function by assigning this function into a collection, environment or global variable as follows:

    • Create a collection variable, i.e. global_func
    • In the variable value write this code,

    (number)=> { return number * number }

    to reuse this function elsewhere in your collection

    let numberSquared = eval(pm.variables.get('global_func'))(5)
    

    now, numberSqaure variables has a value of 25

    ================================

    if you need to declare a function library: you can create a collection variable and assign it this piece of code:

    ({
        print:function() {
            console.log('hello Postman')
        },
        squared:function(number) {
            return number * number
        }
    })
    

    Note: the functions have been enclosed with parentheses

    to reuse this library:

    let lib = eval(pm.variables.get('global_func'))
    lib1.print()
    console.log(lib1.squared(4))
    

    Good luck :)

提交回复
热议问题