# Setting Path Variables in Pre-request Script

**URL:** <https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591>\
**Category:** Help Hub\
**Tags:** variables, pre-request-script\
**Created:** [April 15, 2025, 7:31pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591 "2025-04-15T19:31:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 15, 2025, 7:31pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/1 "2025-04-15T19:31:25Z")

</div>

This is a follow-up to [this post from December](https://community.postman.com/t/updating-path-variable-in-url-before-posting/72344). I have been using this implementation in a Postman web app collection for the past month or so, but at some point in the last week it stopped working.

Now when I make a request using the solution in the link above, I get

```auto
There was an error in evaluating the Pre-request Script:TypeError: pm.request.url.toJSON is not a function

```

Is this a recent/intended change in Postman to disallow this method of accessing the request path variables? If so, what is the new strategy to accomplish this functionality?

I wish I could just go with the “replace path variable with {{variable}} in request” route, but we’re using a collection from a 3rd party for their API that has hundreds of requests, all with the same 1-2 path variables. I can export the collection and edit the JSON file to switch things over, but that’s not a great solution for my team—a prerequest script is a much better solution as its easier to setup.

---

<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:** [April 16, 2025, 10:16am UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/2 "2025-04-16T10:16:47Z")

</div>

Hey @lunar-module-opera10 👋

Welcome to the Postman Community! :postman:

Would you be able to share more details about the URL and the full pre-request script you are using that causes the error, please?

You can mask all the sensitive data, we just need to see the structure of the URL and the code in the script. 🙏

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 1:45pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/3 "2025-04-16T13:45:14Z")

</div>

Hey, Danny! Here’s the script I’m using:

```auto
// automatically set API Version
const version = pm.collectionVariables.get("apiVersion");
pm.request.headers.upsert({ key: "X-Gusto-API-Version", value: version });

// helper method for automatically managing paths
const setPathVariableIfPresent = (key, value) => {
    const url = pm.request.url.toJSON();
    const index = url.variable.findIndex(item => item.key === key);
    if (index >= 0) {
        url.variable[index].value = value;
        pm.request.url = url;
    }
}

// check to see if the request is overriding these settings
const override = pm.request.url.query.all().some(param => param.key === 'override');
if (!override) {
    // set company id on relevant requests based on current environment 
    const companyId = pm.environment.get("gusto_company_id");
    if (companyId) setPathVariableIfPresent("company_id", companyId);

    // set employee id on relevant requests based on current environment 
    const employeeId = pm.environment.get("gusto_employee_id");
    if (companyId) setPathVariableIfPresent("employee_id", employeeId);
    }
}

```

These are my environment and collection variables, respectively:

 ![Screenshot 2025-04-16 at 8.16.27 AM](https://us1.discourse-cdn.com/getpostman/original/3X/8/7/876bfe38c5c2b438b46b50bae8770e53f1554e70.png)  
 ![Screenshot 2025-04-16 at 8.19.06 AM](https://us1.discourse-cdn.com/getpostman/original/3X/6/3/6389ae72554acb474d278243549c37910f1b2126.png)

Here’s what the request looks like (the goal was to setup a pre-request script that would automatically populate the employee\_id and company\_id path variables if they are present):

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/4/6421dc26bff6696c8802e7f69e1ca8aa89b01a8c.png)

Bizarrely, though, I am seeing this morning that my employee endpoint seems to work

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/4/440ef6cb01a3bd4b0b0f6ea51c641c014eafbf6f.png)

---

<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:** [April 16, 2025, 2:00pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/4 "2025-04-16T14:00:09Z")

</div>

How does the _override_ param fit into the URLs being run?

> [@lunar-module-opera10](#):
>
> `const override = pm.request.url.query.all().some(param => param.key === 'override');`

Is that something that you either add or don’t add manually?

For the request that was working this morning, does that append everything correctly to the URL? I can’t tell as I don’t see the console output for the request.

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 2:10pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/5 "2025-04-16T14:10:16Z")

</div>

It’s a way for a user to bypass the automatic path variable settings in the script so they can use hard-coded values in the request like this:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/6/86d5afa9c8fd00b88e72f8fef75f9b555b8b1644.png)

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 2:18pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/6 "2025-04-16T14:18:05Z")

</div>

~~Yeah I’m super confused now because the employee endpoint is returning a 404 again (not throwing the Javascript error), and it’s just not replacing the path variable value now.~~

So for the employee, it looks like I was actually checking the `companyId` on the `if` statement 🤦‍♀️

After fixing that, I’m getting 404s for both as it’s not changing the path variable in either now.

---

<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:** [April 16, 2025, 2:22pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/7 "2025-04-16T14:22:05Z")

</div>

From that line in the code, it’s still going to be `true` if it’s there but it’s been disabled, right? It would only be `false` if it’s completely removed.

This doesn’t mean anything for the overall problem you’re facing, I was just trying to get that clear in my mind.

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 2:28pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/8 "2025-04-16T14:28:00Z")

</div>

When it’s disabled, it’s not added to the request string at all:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/2/921c139989990af856ca6a0a7b79c54c85533167.png)

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 2:35pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/9 "2025-04-16T14:35:02Z")

</div>

Ah it looks like there might be a new method of changing the variables. This seems to work 🤷‍♀️

```auto
const setPathVariableIfPresent = (key, value) => {
    const variables = pm.request.url.variables;
    if (!variables) return;

    const variable = variables.find(v => v.key === key);
    if (variable) {
        variable.value = value;
    }
}

```

---

<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:** [April 16, 2025, 2:40pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/10 "2025-04-16T14:40:42Z")

</div>

It’s still `true` as per that script though.

Param enabled:

 ![Screenshot 2025-04-16 at 15.38.18](https://us1.discourse-cdn.com/getpostman/original/3X/2/d/2da5162c6bf77ff25189efa9d122ce1f45d58183.png)

Param disabled:

 ![Screenshot 2025-04-16 at 15.38.32](https://us1.discourse-cdn.com/getpostman/original/3X/a/2/a2368b6c5e02615045239a2cd8f60dfdd7c91853.png)

Param removed:

 ![Screenshot 2025-04-16 at 15.38.45](https://us1.discourse-cdn.com/getpostman/original/3X/6/3/63c3e8e0734e5e7c386491b22cda78a0e917d2ca.png)

---

<div class="post-metadata">

**Author:** ![lunar-module-opera10](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/lunar-module-opera10/32/32237_2.png) [@lunar-module-opera10](https://community.postman.com/u/lunar-module-opera10)\
**Post date:** [April 16, 2025, 2:43pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/11 "2025-04-16T14:43:42Z")

</div>

Ah yeah, it looks like I need to update my override check to this:

```auto
const override = pm.request.url.query.find(param => param.key === 'override');
if (!override || override.disabled) {
 // settings
}

```

The previous script I posted clears up the “not a function” error I was getting, so now everything appears to be working as expected 😁

---

<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:** [April 16, 2025, 2:53pm UTC](https://community.postman.com/t/setting-path-variables-in-pre-request-script/78591/12 "2025-04-16T14:53:53Z")

</div>

Awesome! Glad to see you have it all working! 🏆
