# Cannot use variables in GET requests

**URL:** <https://community.postman.com/t/cannot-use-variables-in-get-requests/17110>\
**Category:** Help Hub\
**Created:** [October 26, 2020, 9:29pm UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110 "2020-10-26T21:29:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sebi](https://community.postman.com/u/Sebi)\
**Post date:** [October 26, 2020, 9:29pm UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/1 "2020-10-26T21:29:31Z")

</div>

I have a collection runner that uses a json file to read variable values:

> <https://stackoverflow.com/questions/64544676/postman-collection-runner-does-not-apply-data-to-requests>

I cannot seem to use them when creating GET requests:

URL:

[http://localhost:3000/agents/{{gadget\_id}](http://localhost:3000/agents/%7B%7Bgadget_id%7D)}

Pre-request script:

var gadget\_id = pm.iterationData.get(“gadget\_id”);

The request URL for all get requests in the collection runner is:

[http://localhost:3000/agents/](http://localhost:3000/agents/)

NO VARIABLE VALUE HAS BEEN ASSIGNED. WHY?

---

<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:** [October 26, 2020, 9:39pm UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/2 "2020-10-26T21:39:50Z")

</div>

Hey @Sebi

Can you provide some screenshots so we can see what’s going on please?

Do any of the request tabs have orange dots on them? If they do, you need to save those so the runner knows about those changes.

---

<div class="post-metadata">

**Author:** ![Sebi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sebi](https://community.postman.com/u/Sebi)\
**Post date:** [October 27, 2020, 10:11am UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/3 "2020-10-27T10:11:22Z")

</div>

> Can you provide some screenshots so we can see what’s going on please?

 ![postman](https://us1.discourse-cdn.com/getpostman/original/3X/8/8/88848d08d9a9e172059b0bb5574d1f2402220350.jpeg)

 ![postman-1](https://us1.discourse-cdn.com/getpostman/original/3X/a/b/abe7f26ad6eaa5a951cc5c0a23acb626ed41f02c.jpeg)

> Do any of the request tabs have orange dots on them? If they do, you need to save those so the runner knows about those changes.

Everything’s saved (green).

Edit:

In these screenshots “gadget\_id” has been replaced with “agent\_id” but the requests are identical this aside.

---

<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:** [October 27, 2020, 10:13am UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/4 "2020-10-27T10:13:10Z")

</div>

Could expand the view of the request builder to show the request tabs, do any of these have orange dots on them. Not sure what you mean by ‘green’ the tab shouldn’t show any colour.

Have the created a new run or are you retrying the same one as before?

---

<div class="post-metadata">

**Author:** ![Sebi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sebi](https://community.postman.com/u/Sebi)\
**Post date:** [October 27, 2020, 10:20am UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/5 "2020-10-27T10:20:27Z")

</div>

Ok. It works after reimporting the data file. But there’s still a problem; it creates requests even when the agent\_id is empty. Is it possible to set conditions on the requests (drop request is variable value if null/empty)?

E.g:

```auto
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "9d788283-87a9-4c38-8d48-30ddc1604058"
},

```

Don’t make request for gadget\_id like

[www.url.com/gadgets/{{gadget\_id}](http://www.url.com/gadgets/%7B%7Bgadget_id%7D)}

gadget\_id is empty so don’t even make the request.

Something like this:

> [@How Can One Terminate Executing a Request From Within Pre-Request Script?](https://community.postman.com/t/how-can-one-terminate-executing-a-request-from-within-pre-request-script/7432):
>
> When I go to execute a request, I use logic within the Pre-request Script to determine IF I really want to execute this request or not based on environment variable settings. I don’t seem to be able to find a way of specifically terminating execution prior to the invocation of the actual API call and subsequent Tests scripts. In short, that is what I want to do, stop prior to the actual API call. I tried using postman.setNextRequest(null) but it doesn’t stop execution at that time and still go…

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [October 27, 2020, 11:35am UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/6 "2020-10-27T11:35:18Z")

</div>

Something that I’ve done to handle this scenario is just to change the url the request is hitting via the prerequest script. Let’s take an example.

Say you don’t want to execute the request when `gadget_id` is null. You could do something like this in your prerequest script;

```auto
const gadgetId = pm.iterationData.get('gadget_id');
if(!gadgetId) {
  pm.request.url = 'https://postman-echo.com/delay/0';
  pm.request.method = 'GET';
  pm.variables.set('skip', true);
}
else {
  // If you need to do any other preprocessing if gadget_id exists
}

```

This will still execute a request, but instead of hitting your API, it will hit the postman echo api.  
Since you don’t want tests to run (because your api didn’t actually run), you’d want to add this to your tests tab:

```auto
const skip = pm.variables.get('skip');
if(!skip) {
  // all of your tests for your API
}

```

Essentially you’re just putting your tests behind an if statement to make sure they aren’t run if you ‘skipped’ the request.

Does this all make sense?

---

<div class="post-metadata">

**Author:** ![Sebi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Sebi](https://community.postman.com/u/Sebi)\
**Post date:** [October 28, 2020, 2:10pm UTC](https://community.postman.com/t/cannot-use-variables-in-get-requests/17110/7 "2020-10-28T14:10:22Z")

</div>

I’ve figured it out (created tests based on the entries in the data file (expect to fail and expect to pass)) based on what’s being read (in this case the gadget\_id).
