# Why can't (How can) PRE-REQUEST support {{variable}} in request-line / request-body?

**URL:** <https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595>\
**Category:** Help Hub\
**Tags:** pre-request\
**Created:** [September 4, 2019, 5:32am UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595 "2019-09-04T05:32:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![medsonkissinger](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@medsonkissinger](https://community.postman.com/u/medsonkissinger)\
**Post date:** [September 4, 2019, 5:32am UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/1 "2019-09-04T05:32:50Z")

</div>

**Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-help-category/38) before creating a new issue.**

**Describe the bug**  
Why can’t (How can) PRE-REQUEST support {{variable}} in request-line / request-body ?

**To Reproduce**  
Steps to reproduce the behavior:

1. save ‘e\_http’ as environment-variable, like ‘[http://1.1.1.1](http://1.1.1.1)’
2. fill request-line as **{{e\_http}}** /api/v1/sys/auth/login/userLogin
3. fille pre-request script as

> var requestUrl = request.url;  
> var requestUrl2 = “{{e\_http}}/api/v1/sys/auth/login/userLogin”;
> 
> var md5str = CryptoJS.MD5(requestUrl).toString();  
> var md5str2 = CryptoJS.MD5(requestUrl2).toString();
> 
> console.log(requestUrl+“///”+md5str);  
> console.log(requestUrl2+“///”+md5str2);

1. See screenshots, variable can not be referenced in pre-request:  
 ![postman_20190904110848](https://us1.discourse-cdn.com/getpostman/original/3X/f/2/f29db0ffd7d20e61caf5a0db5ee4cfe9ad5ab997.png)

**Expected behavior**  
reference to variable in pre-request when variable in request-line / request-body

**Screenshots**  
compare with jmeter:

 ![jmeter_jmeter_20190904130020](https://us1.discourse-cdn.com/getpostman/original/3X/f/f/ff86ac3265861118500db46b17143056ad98f335.png)

**App information (please complete the following information):**

- App Type [Chrome App]
- Postman Version [7.6.0]
- OS: [windows 7]

**Additional context**  
Add any other context about the problem here.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 4, 2019, 6:11am UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/2 "2019-09-04T06:11:51Z")

</div>

Hey @medsonkissinger,

Welcome to the Postman community 🏆

Within the `Pre-request Script`, you cannot use the `{{...}}` syntax directly so you would need to grab that environment variable in either of the following ways:

```
`${pm.environment.get('e_http')}/api/v1/sys/auth/login/userLogin`  

```

Or

```
`${pm.environment.replaceIn('{{e_http}}')}/api/v1/sys/auth/login/userLogin`

```

Hope that helps. 🙂

---

<div class="post-metadata">

**Author:** ![medsonkissinger](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@medsonkissinger](https://community.postman.com/u/medsonkissinger)\
**Post date:** [September 4, 2019, 12:19pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/3 "2019-09-04T12:19:03Z")

</div>

Very thank you for your answer @danny-dainton .  
I understand what you mean, but there is another problem comes to me :  
If I given up using **request.url/request.data** , **I have to customize over 100 apis**  
100 request-urls is easy, 100 request-bodies will drive me crazy 😫

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 4, 2019, 12:50pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/4 "2019-09-04T12:50:47Z")

</div>

I’m a little bit confused by what you mean, the original question was around being able to use an environment variable in a `Pre-request Script`.

This slight change in what you had _would_ allow you to do that:

```
var requestUrl = request.url;
var requestUrl2 = `${pm.environment.get('e_http')}/api/v1/sys/auth/login/userLogin`;

var md5str = CryptoJS.MD5(requestUrl).toString();
var md5str2 = CryptoJS.MD5(requestUrl2).toString();

console.log(requestUrl+"///"+md5str);
console.log(requestUrl2+"///"+md5str2); 

```

This is only for a _single_ request but this could be moved up to the Collection Level and you would only need it once for all the requests, rather than repeating it in everyone.

As for the `request body` part of your follow up question, I don’t know what your collection looks like or what other scripts/things you have going on in there so I can only offer a solution based on a _best guess_.

You would need to elaborate more on your specific use case and maybe add some more example images for other people to look at and provide a more tailored answer. 🙂

---

<div class="post-metadata">

**Author:** ![medsonkissinger](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@medsonkissinger](https://community.postman.com/u/medsonkissinger)\
**Post date:** [September 4, 2019, 3:02pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/5 "2019-09-04T15:02:17Z")

</div>

it’s so nice for you to reply me in time. and i am sorry for not giving you all of my code because it’s classify for my company.now pls allow me to show you the demo:

 ![postman_20190904223832](https://us1.discourse-cdn.com/getpostman/original/2X/c/c0aab3c8efa117110f042f25726f9897dc0e953d.png)  
at last , i will put **signKey** into request-header.  
after login request, there are over 100 requests to rebuilt.  
btw, my job is Interface Automation Developer.

thank you for reading my code.

here is the script:

```
var lodash = require(`lodash`);
const querystring = require('querystring');
console.log("------------------------");
var requestMethod = request.method;
var requestUrl = request.url;
var requestData = request.data;
//generate signKey by CryptoJS
var signKey = signRequest(requestMethod,requestUrl,requestData);
pm.environment.set("signKey", signKey);
console.log("signKey:"+signKey);

/**
 * @param {} method 
 * @param {} url 
 * @param {} data 
 */
function signRequest (method , url , data) {
  let signData = new Map()
  let signString = '' 
  let keys = []
  getSignData('', data, signData)
  signData.forEach((value, key) => keys.push(key))
  keys.sort().forEach((key) => {
    signString += `${key.toString()}=${signData.get(key) ? signData.get(key).toString() : ''};`
    }
    )
  return CryptoJS.SHA256(signString).toString()
}  
//format reuqest body 
function getSignData (keyPrefix, obj, signData) {
  if (lodash.isMap(obj)) {
    obj.forEach((value, key) => {
      getSignData(keyPrefix ? `${keyPrefix}.${key}` : `${key}`, value, signData) 
    })
  } else if (lodash.isSet(obj) || lodash.isArray(obj)) {
    let idx = 0
    obj.forEach((item) => {
      getSignData(keyPrefix ? `${keyPrefix}.${idx}` : `${idx}`, item, signData) 
      idx++
    })
  } else if (isPrimitive(obj)) {
    signData.set(keyPrefix, obj)
  } else if (lodash.isObject(obj)) {
    Object.getOwnPropertyNames(obj).forEach((name) => {
      getSignData(keyPrefix ? `${keyPrefix}.${name}` : `${name}`, obj[name], signData) 
    })
  } 
}
//format reuqest body 
function isPrimitive (obj) {
  return lodash.isNumber(obj) || lodash.isString(obj) || lodash.isBoolean(obj) || lodash.isDate(obj)
}
```

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 25, 2021, 3:27pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/6 "2021-01-25T15:27:16Z")

</div>

Hello @danny-dainton  
I tried to insert variables like you mentioned, but still does not work, i tried both methods - still nothing ☹

```
// Getting auth token from Keycloak
    pm.sendRequest({
    url:"https://${pm.globals.replaceIn('{{KC_HOST}}')}:${pm.globals.replaceIn('{{KC_PORT}}')}/auth/realms/${pm.globals.replaceIn('{{KC_REALM}}')}/protocol/openid-connect/token", 

        method:"POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Basic " + btoa("rest_api:password")
        },
        body: {
            mode: "urlencoded",
            urlencoded: [
                { key: "grant_type", value: "password" },
                { key: "username", value: "user" },
                { key: "password", value: "password" },
            ]
        }
    },

```

Hostname being sent is  
Host: {pm.globals.replacein('{{kc\_host}}')}:{pm.globals.replaceIn(’{{KC\_PORT}}’)}  
So it did not work ☹  
If i replace variables with IP and port numbers and realm name respectively, it does work

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 25, 2021, 4:30pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/7 "2021-01-25T16:30:38Z")

</div>

If those are global variables, you wouldn’t really need to use `replaceIn` - You also need to ensure that these strings use `backticks` (`) and not `quote marks` at each end (") or the variables will not be resolved.

```auto
// Wrap the url value in backticks
url:`https://${pm.globals.get('KC_HOST')}:${pm.globals.get('KC_PORT')}/auth/realms/${pm.globals.get('KC_REALM')}/protocol/openid-connect/token`

```

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 25, 2021, 4:35pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/8 "2021-01-25T16:35:50Z")

</div>

Thanks @danny-dainton !  
I changed string like you said and now it works! - I did some other mistakes thus it did not work, but i found them myself. Thank you a lot! 🙂

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 25, 2021, 4:38pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/9 "2021-01-25T16:38:42Z")

</div>

Cool, I would check to see what key/values you have set globally, to ensure those are what you’re expecting.

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 25, 2021, 4:39pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/10 "2021-01-25T16:39:42Z")

</div>

Yeah, i pressed “set variable” button during my messing arround and set value of KC\_HOST to … “kc\_host” 😃 But i found it out. Now it works! Thank you very much!

---

<div class="post-metadata">

**Author:** ![vkbeaudreau](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@vkbeaudreau](https://community.postman.com/u/vkbeaudreau)\
**Post date:** [June 23, 2023, 3:15pm UTC](https://community.postman.com/t/why-cant-how-can-pre-request-support-variable-in-request-line-request-body/7595/11 "2023-06-23T15:15:59Z")

</div>

I ran into the same problem, but with btoa. Using the back-ticks as recommended here solved that. Thanks!
