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

**URL:** <https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321>\
**Category:** Help Hub\
**Created:** [July 21, 2020, 7:50am UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321 "2020-07-21T07:50:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdxcz](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@cdxcz](https://community.postman.com/u/cdxcz)\
**Post date:** [July 21, 2020, 7:50am UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/1 "2020-07-21T07:50:36Z")

</div>

I want to mock exact request values in response.

ex :

**request body**

```auto
{“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)

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

```

I have tried adding changes in prescript

```auto
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

```auto
{"username":pm.globals.usrName}

```

but when i ran response body is coming as :

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

```

expected output:

{“username”:“google”}

please suggest me the possible ways. any help much appreciated

---

<div class="post-metadata">

**Author:** ![benonsen](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@benonsen](https://community.postman.com/u/benonsen)\
**Post date:** [July 28, 2022, 11:59am UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/2 "2022-07-28T11:59:30Z")

</div>

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

Best regards

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 28, 2022, 1:23pm UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/3 "2022-07-28T13:23:48Z")

</div>

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…

> **[Dynamic variables | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/variables-list/)**
>
> Dynamic variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

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

---

<div class="post-metadata">

**Author:** ![benonsen](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@benonsen](https://community.postman.com/u/benonsen)\
**Post date:** [July 28, 2022, 1:38pm UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/4 "2022-07-28T13:38:08Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 28, 2022, 3:16pm UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/5 "2022-07-28T15:16:09Z")

</div>

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](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts).

---

<div class="post-metadata">

**Author:** ![benonsen](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@benonsen](https://community.postman.com/u/benonsen)\
**Post date:** [July 29, 2022, 6:54am UTC](https://community.postman.com/t/how-to-store-a-request-json-in-a-variable-and-use-it-in-the-mocked-response-body/14321/6 "2022-07-29T06:54:16Z")

</div>

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)?
