# Extracting Variables via Utility Methods

Now, with the help of utility methods, you can extract the variables of your test case response. This functionality is helpful in extracting variables

  • from any type (JSON/XML or other than JSON/XML) of test case response.
  • or from response headers
    • An example use case is performing token-based authentication in vREST.

# How to use:

  1. First define a utility method in Configuration Tab >> Utility Methods section. vREST automatically adds the following in the function context (via this):
    • this.variables
      This contains the variable map for global and extracted variables.
    • this.methods
      This contains the methods map. Through this, you may invoke other utility methods.
    • this.execution
      Only available if you invoke the utility methods inside the Variable Extractor tab. This contains the API request and response details. Some important information:
      1. If you want to access the response content then you may use this.execution.response.content.
      2. If you want to access the parsed response content then you may use this.execution.response.parsed.
      3. If you want to access the status code then you may use this.execution.response.statusCode.
      4. And if you want to access the response headers then you may use this.execution.response.headers.

For more information about the structure of the above properties, please read our guide on Context Parameters.

You may write your utility method like below:

  1. Use this utility method in the variable extractor tab like this.

In the above figure, value of var1 will be extracted via utility method varExtractUtility. Here value "hello" will be passed to "userDefinedParam1" parameter and "world" will be passed to "userDefinedParam2" parameter of the utility method defined in step 1.