Using backtick syntax for dynamic session token handling

When a script includes content that changes every time the script is played, you can use backtick syntax to define static values that need to be replaced with the current value at runtime. When the Mobile agent finds a backtick string such as `textToReplace`, it replaces the string with the dynamic value.

A backtick string can be used to capture a session token that changes with each login, for example as a security measure for banking applications.

The backtick syntax is supported in headers and URL text fields. It is not supported in the body of HTTP requests.

The following procedure shows how to use backtick syntax to handle dynamic session tokens.

In the Recorder, select File > New Script to manually create a script.

To insert an HTTP action into an existing script, open the script open icon from a local directory or the Dynatrace Portal.

Create an HTTP action.

In the script timeline, hover over the action separator Action separator until it changes to the add icon Add icon. Click this icon to display the action menu, and select HTTP to add the action.

Add HTTP action

The HTTP Requests tab for the new action is displayed.

HTTP request tab

Add the URL to be used for extracting the session token.

Click the add icon add icon to insert the row for defining the GET request for the URL.

GET request

Click More to specify parameters and/or payload data, then select the Reply Handler tab.

Reply handler tab

Add the reply handler code.

For this example, a session token is returned in XML format when a username and password are passed to the URL added above. The response may look like the following code.

 <Login>
   <user>
      <sessionToken>1234567</sessionToken>
   </user>
</Login>

Use the reply handler of the HTTP request to extract the session token from the response. Add the following code to the Reply Handler tab. Click OK to save the code and return to the HTTP Requests tab.

var xmlText = response.getBodyString();
var parser = new external.getMainWindow().DOMParser();
xmlDoc = parser.parseFromString(xmlText, "text/xml");
var token = xmlDoc.getElementsByTagName("sessionToken")[0].firstChild.nodeValue;
alert(token);
parameters.sessionToken = token;

reply handler code

In the URL field for the request, add the HTTP call as shown in this example.

Note that parameters['sessionToken'] is surrounded by backticks ( ` ) and sessionToken is surrounded by single quotes ( ' ).

HTTP call

Save the script.

Finish the script if you are manually creating a script.

Click Save save icon to save the script locally or to the Dynatrace Portal.

Each time you run the script, the current value of sessionToken is captured and inserted in the script.