0

I want to mock exact request values in response.

ex :

request body

{“username”:“google”,
“address” : {“city”:“CA”},
“contactNo”:“8274287844”
}

response should be like this : based on request i need to map exact values to same key(from request to response - mocking with example)

{“username”:“google”,
“address”:{“city”:“CA”}
}

I have tried adding changes in prescript

var reqBody= pm.request.body.raw
console.log(reqBody)
//console.log(req)
var json_verify = function(reqBody){ try { JSON.parse(reqBody); return true; } catch (e) { return false; }}
if(json_verify){
    var val = JSON.parse(pm.request.body.raw)
    var bol = true
    for(var key in val)
    {
     if(key === "username"){
       pm.globals.set("userName", key);
     }
   }
}

my example mocked response body like this :

{"username":{{userName}}} or tried {"username":{{$userName}}} or tried

{"username":pm.globals.usrName}

but when i ran response body is coming as :

{"username":{{userName}}} or {"username":{{$userName}}} or {"username":pm.globals.userName}

expected output:

{"username":"google"}

please suggest me the possible ways. any help much appreciated.

var a
  • 39
  • 9
  • any help pls .... – var a Jul 21 '20 at 07:22
  • See my answer here (https://stackoverflow.com/questions/62758297/is-there-any-way-to-run-a-test-script-after-each-request-in-postman-collection/62776603#62776603). By using the pre-request script you can capture some data and test it after the process. The same functionality exists on the individual request level as well. – Steven Scott Jul 21 '20 at 19:08
  • @StevenScott i want to form the response using request body value. curently i am using prescipt and capturing values from req and setting to global variables using these variable in response object. but its not wrking. pls check my question i have specified clearly. – var a Jul 22 '20 at 12:42
  • pls help me on this or is there any other tool that support this type situation – var a Jul 22 '20 at 12:50

1 Answers1

0

I have figured it out. {"username":{{$userName}}} and {"username":pm.globals.usrName} will work. please follow this

Is there any way to run a test script after each request in Postman Collection?

it will guide you through some basics

posted lately but to help any one who may face, this will helpful

var a
  • 39
  • 9