Search code examples
scriptingpostmanpostman-collection-runner

postman: changing pre-request script dyncamically during a collection run


There are certain functions I would like to run as pre-request script for certain requests and not others.I would like to do this without having to repeat the code for all requests using the UI, but in one place - maybe the pre-request script for the collection. Does the sandbox allow access to the runtime, so that I can maybe defined this as the pre-request script of the first request.

something like currentRun.collection['someRequest'].pre = () => {console.log("hello world!")}


Solution

  • Click the collection and add pre-request script there .

    inside collection pre-request , you can add if condition like

    if (pm.info.requestName==="request1"){
    
       do this
    
    } else if (pm.info.requestName==="request2"){
    
       do this
    }
    

    Another option is to save the function as environment variable and call eval on it:

    eval(pm.environment.get("variablename"))