# Extracting Variables from API Response

Variables can be extracted from Test Case's responses and can be used in subsequent HTTP requests (test cases). To extract the variable's values from the test case's actual results, please follow the steps below:

  • Open the test case from which you want to extract variables
  • Go to the "Variable Extractor" sub-tab
  • In the JSON Path / XML Path / Utility Method column, you may provide
    • the JSON path of the API response if the response is in JSON format. For more information, please read JSON Path Syntax.
    • the XML path of the API response if the response is in XML format. For more information, please read XML Path Syntax.
    • utility method to extract data from any type of response. For more information, please read Extracting variable via utility methods.
  • in the Post Validation? column, By default the value is set to false, which means, the variable will be extracted before the response validation process. And you may use the extracted variables in the same test case during the response validation process. If you enable this flag then the variable will be extracted after the response validation process.

Let us take an example, how we can extract a variable from API response:

  • Suppose we have an API that creates resources on the server and returns the following JSON response:
{
  "id": "54a79b704cba8d5328d087f5",
  "resource_name": "testcase",
  "resource_url": "http://vrest.io/i/demo/m/RVD/create_resource",
  "resource_description": "This API creates a resource on the server",
  "meta": {
    "created_at": "2015-01-03T07:41:21.000Z"
  }
}
  • Now, we want to save the id of newly created resource into variable say "resourceId" and creation time into variable say "resourceCreatedAt". We can extract these variables in the following way:

Few points regarding writing Path in the above table:

  • Each individual property value can be extracted via JSON path notation e.g. id or meta.created_id

  • For more information, read JSON Path syntax.

  • Now we can use these extracted variables in subsequent requests. Note that once a variable is defined, it can be used in all subsequent requests within that test run only. If you want to override this variable, simply re-define the variable in any request.

Suppose we have an API that updates this newly created resource and it needs the ID of the resource to update. We can use the {{resourceId}} variable (extracted in the previous step) in the URL as shown in the following figure:

Notes:

  • Variable extraction feature is only applicable for JSON / XML responses at present using JSON / XML path.
  • For defining JSON path, follow the guide.
  • For defining the XML path, follow the guide.
  • If you want to extract variables from responses other than JSON/XML, then you need to write your own utility method to extract data from either response body or response headers. For more information on this, please read our guide on Extracting variable via Utility Methods.