apiserverpostman

Is there a way for postman to read the response data from a GET request and then use an IF THEN statement to run a POST request?


At the time of writing this question, I was attempting to get Postman to send a GET request, Read the response, and use that response to send a POST Request.

I am not sure how to accomplish this. I am not sure where the data of the GET request is stored, and I do not know how to access it.

Where is this data stored, if anywhere, and how can I use it?


Solution

  • In pre-requisite add:

    // set initial value
    const method = pm.variables.get("method")
    // set initial value as GET if method is undefined
    method ? null : pm.variables.set("method", "GET")
    
    // Set this as method
    pm.request.method =method
    

    in test script add :

    // the condition check
    if (pm.response.json().somevalue === "somevalue") {
        
        //then change the method
        pm.variables.set("method", "POST")
    
        //call the same request again using setNExtRequest
        // pm.info.reqeustName gives current request's name
        postman.setNextRequest(pm.info.requestName)
    }