How to store a request JSON in a variable and use it in the mocked response body

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

Have you found any solution to this problem? I am currently facing almost the same problem.

Best regards

Hi @benonsen

Not sure I have the answer you are looking for. But I do know that the faker library works well with the examples files.

You should be able to use any of the random/dynamic variables from this page…

If I find an answer for using your own variables (which should work from what I have read), I will update the thread.

Hi @w4dd325!
Thanks for your reply. So, basically my problem is:

  • I get through the request body a value passed

  • then I need to manipulate the value (just add a fixed string to the passed string)

  • at the end, I need to return the new calculated value through the response body

Is this even possible in one request? My idea was to split it into two calls, whereas the first stores & manipulates the value via the tests-script:

var data = JSON.parse(pm.request.body.raw);
// manipulate the string
pm.environment.set("reqbody", data.content);

And the second call returns the new value via a dynamical variable in the response body.

I’m not 100% I follow what you are trying to do, but you could possibly run the first call in a pre-request script and then grab the data from there, you could then pass the data into the request body and submit it in the same call.
This would be done using pm.sendRequest.

If I understand your approach correctly, you would create one request, which calls another request in the pre-request script to fetch the data?
My problem is, that I get data passed via the request body. Then I need to modify this data and then returning it back via the response body.

Let’s assume we have a request x, which returns the modified data. In order to work, the request x needs the data passed to modify it.
So with your idea, we would call the request x in the pre-request script and fetch the result and then passing it to the response-body of the original request. But this will not work, because the request x needs the data in the request-body, which at this moment we don’t have, because it is passed with the original request.

To summarize my task:
I need to create a mock server, which simulates signing a xml-file (base64). Basically, I get the base64 passed via the request and need to add the signature to the file. To make things easier, I always add the same hard-coded string signature to the file. After this step, I need to return the signed base64 file with the response header. Is this even possible with postman (with one request)?