# How do I access request body in pre-request test script with values assigned to variables in request body?

**URL:** <https://community.postman.com/t/how-do-i-access-request-body-in-pre-request-test-script-with-values-assigned-to-variables-in-request-body/30451>\
**Category:** Help Hub\
**Tags:** variables, pre-request, request-body\
**Created:** [December 7, 2021, 6:22am UTC](https://community.postman.com/t/how-do-i-access-request-body-in-pre-request-test-script-with-values-assigned-to-variables-in-request-body/30451 "2021-12-07T06:22:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crescent-sushant](https://avatars.discourse-cdn.com/v4/letter/c/a587f6/32.png) [@crescent-sushant](https://community.postman.com/u/crescent-sushant)\
**Post date:** [December 7, 2021, 6:22am UTC](https://community.postman.com/t/how-do-i-access-request-body-in-pre-request-test-script-with-values-assigned-to-variables-in-request-body/30451/1 "2021-12-07T06:22:56Z")

</div>

How do I access request body in pre-request test script with values assigned to variables in request body?

right now when I do:  
var body = pm.request.body.raw;  
console.info(“request body:”+body );  
In console I see:  
request body:{↵  
“ID”: 1,↵  
“something”: xyz,↵  
“token”: “{{token}}”,↵  
“timestamp”: {{unix\_timestamp}}↵

So How do I access the request body with actual values assigned to the variables?

I know, I could just do string replace for individual requests. But I have assigned the script to a folder and was expecting it to work for every request in the folder.

Thanks.

---

<div class="post-metadata">

**Author:** ![makaleem1986](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@makaleem1986](https://community.postman.com/u/makaleem1986)\
**Post date:** [December 8, 2021, 3:11pm UTC](https://community.postman.com/t/how-do-i-access-request-body-in-pre-request-test-script-with-values-assigned-to-variables-in-request-body/30451/2 "2021-12-08T15:11:32Z")

</div>

Looks like postman… doesn’t resolve the variables … when the request is in pre-requisite tab. I had the same problem and I wrote some javascript function  
Usage replaceAllVariables(body).

```auto
function replaceAllVariables(str)
{
    var totalIndexes=getIndexes(str);
    var i=0;
    for(i=0;i<totalIndexes.length;i++)
    {
       str= replaceVariables(str);
    }
    return str;
}
function getIndexes(str)
{
  var a=[],i=-1;
  while((i=str.indexOf("{{",i+1)) >= 0) a.push(i);
return a;
}
function replaceVariables(str)
{
    	var startIndex=str.indexOf("{{");
	 if(startIndex!==-1)
  {
  var n = str.indexOf("}}");
  var substring=str.substring(startIndex+2,n);
  var value=postman.getEnvironmentVariable(substring);
  str=str.replace("{{"+substring+"}}",value);
  }
  return str;
}

```
