# How to send a GET request by constructing the url dynamically with a global vairable?

**URL:** <https://community.dynatrace.com/t/how-to-send-a-get-request-by-constructing-the-url-dynamically-with-a-global-vairable/47616>\
**Category:** Synthetic Monitoring\
**Tags:** synthetic-monitoring\
**Created:** [September 12, 2024, 4:45am UTC](https://community.dynatrace.com/t/how-to-send-a-get-request-by-constructing-the-url-dynamically-with-a-global-vairable/47616 "2024-09-12T04:45:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pseg1984](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@pseg1984](https://community.dynatrace.com/u/pseg1984)\
**Post date:** [September 12, 2024, 4:45am UTC](https://community.dynatrace.com/t/how-to-send-a-get-request-by-constructing-the-url-dynamically-with-a-global-vairable/47616/1 "2024-09-12T04:45:18Z")

</div>

Hi,

i have a synthetic test with POST method which creates an account.

i save the account\_id in a global variable with this line:

var reponseBody = response.getResponseBody();

var jsonData = JSON.parse(responseBody);

api.setValue(“account\_id”, jsonData.id)

Then i have a second test with GET method, here i need to construct the url by appending the account\_id:

this is what i did:

In the first field for http request url i write this:

[https://myapp.test.api/create\_accout/{account\_id}](https://myapp.api/create_accout/%7Baccount_id%7D)

HTTP method = GET

pre-execution script is enabled and i wrote this:

var account\_id = api.getValue(“account\_id”);

var url = request.getUrl();

url = url.replace(“[{account\_id}](https://myapp.api/create_accout/%7Baccount_id%7D)”, account\_id);

request.setUrl(url);

When i execute the first test it works well, it creates the account and i save the account\_id in a global variable.

When i execute the second test, it returns UNKNOWN\_METHOD\_ERROR

if i check the error, the request is not sent, the url is empty.

Any help please, this is the first time i’m using dynatrace for synthetic test.

Thank you

---

<div class="post-metadata">

**Author:** ![PacoPorro](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pacoporro/32/36087_2.png) [@PacoPorro](https://community.dynatrace.com/u/PacoPorro)\
**Post date:** [September 12, 2024, 6:29am UTC](https://community.dynatrace.com/t/how-to-send-a-get-request-by-constructing-the-url-dynamically-with-a-global-vairable/47616/2 "2024-09-12T06:29:46Z")

</div>

From the doc:

> **[Pre- and post-execution scripts for HTTP monitors in Classic](https://docs.dynatrace.com/docs/observe/digital-experience/synthetic-monitoring/http-monitors-classic/pre-and-post-scripting-for-http-monitors-classic)**
>
> Learn how to apply pre and post scripts to your requests

_Variables can be passed only in the context of a single execution of an HTTP monitor. You also need to make sure that when you refer to a variable, the data behind it is logically available to the monitor. For example, if you set a variable based on data from the response body of a request, you cannot retrieve the value in the pre-execution script for that request, because at this point, the content does not exist yet._

---

<div class="post-metadata">

**Author:** ![hannahmac](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/hannahmac/32/41823_2.png) [@hannahmac](https://community.dynatrace.com/u/hannahmac)\
**Post date:** [September 12, 2024, 7:42am UTC](https://community.dynatrace.com/t/how-to-send-a-get-request-by-constructing-the-url-dynamically-with-a-global-vairable/47616/3 "2024-09-12T07:42:10Z")

</div>

You can use the credential vault to store values to use in different monitors. You can use the relevant [write data](https://docs.dynatrace.com/docs/shortlink/http-monitor-pre-post-script#write-data) method to store the credential in one monitor and then [api.getCredential(id, type)](https://docs.dynatrace.com/docs/shortlink/http-monitor-pre-post-script#retrieve-data) method to retrieve the value in another monitor.

Is there are a reason you are splitting these activities across monitors? If you need to have the create account monitor run before the other monitor you may need to use workflows to prevent them running in the wrong order.
