12 Sep 2024 05:45 AM
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}
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}", 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
Solved! Go to Solution.
12 Sep 2024 07:29 AM
From the doc:
https://docs.dynatrace.com/docs/shortlink/http-monitor-pre-post-script#variables
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.
12 Sep 2024 08:42 AM
You can use the credential vault to store values to use in different monitors. You can use the relevant write data method to store the credential in one monitor and then api.getCredential(id, type) 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.