Variable Usage in API | Zoho QEngine Help

Variable Usage in API

Prerequisite: Read the help document for variables to understand better.

Pass data in chained API requests

Use variables to extract data from the previous response and feed it to another request in API Testing.


  1. Source: Location from where the values will be extracted. You can select the source as HeaderStatus CodeXML Body, or JSON Body.
  2. Property:
    1. Header as the source: Specify the Header key as the property whose value will be assigned to the variable.
    2. JSON Body and XML Body as the source: Specify the JSON or XML path to extract a value.
  3. Variable: Name of the variable

Examples to extract data from JSON key-value response

  1. {
  2.    "id":1,
  3.    "name":"Leanne Graham",
  4.    "username":"Bret",
  5.    "address":{
  6.       "city":"Gwenborough",
  7.       "geo":{
  8.          "lat":"-37.3159",
  9.          "lng":"81.1496"
  10.       }
  11.    },
  12.    "phoneNumbers":[
  13.       {
  14.          "type":"home",
  15.          "number":"000-111-2222"
  16.       },
  17.       {
  18.          "type":"mobile",
  19.          "number":"111-222-3333"
  20.       },
  21.       {
  22.          "type":"office",
  23.          "number":"333-444-5555"
  24.       }
  25.    ]
  26. }

PropertyValue
usernameBret
address.cityGwenborough
phoneNumbers[0].number000-111-2222
phoneNumbers[-1].number333-444-5555
phoneNumber[*].number000-111-2222 (or) 111-222-3333 (or) 333-444-5555

Examples to extract data from JSON index-value response

  1. [
  2.       {
  3.          "type":"home",
  4.          "number":"000-111-2222"
  5.       },
  6.       {
  7.          "type":"mobile",
  8.          "number":"111-222-3333"
  9.       },
  10.       {
  11.          "type":"office",
  12.          "number":"333-444-5555"
  13.       }
  14.    ]

    PropertyValue
    [1].number111-222-3333
    [-1].typeoffice

XPATH Expressions

The following is a sample for XPath expressions
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.    <address>
  4.       <city>Gwenborough</city>
  5.       <geo>
  6.          <lat>-37.3159</lat>
  7.          <lng>81.1496</lng>
  8.       </geo>
  9.    </address>
  10.    <id>1</id>
  11.    <name>Leanne Graham</name>
  12.    <phoneNumbers>
  13.       <element>
  14.          <number>000-111-2222</number>
  15.          <type>home</type>
  16.       </element>
  17.       <element>
  18.          <number>111-222-3333</number>
  19.          <type>mobile</type>
  20.       </element>
  21.       <element>
  22.          <number>333-444-5555</number>
  23.          <type>office</type>
  24.       </element>
  25.    </phoneNumbers>
  26.    <username>Bret</username>
  27. </root>

Property Value
/root/username/text()Bret
/root/address/city/text()Gwenborough
/root/phoneNumbers/element[1]/number/text()000-111-2222
/root/phoneNumbers/element[3]/number/text()333-444-5555
/root/phoneNumbers/element/number/text()000-111-2222 (or) 111-222-3333 (or) 333-444-5555

Example to store data from the first response and feed it to the second request as a query param

We will make two GET requests in this example. The first request will return a JSON response in which each element contains an "id" key with values starting from 1. We will extract "id" value of the second element (so the extracted value is 2), store it in a variable named var, and use the variable as a query param in the second request.
Run API calls with the following data:

Request 1:

HTTP method: GET



Variables:

Source: JSON Body
Property: [1].id
Variable: var



Request 2:

HTTP method: GET



The following is a truncated view of the first response. The highlighted value of 'id' is what is extracted and supplied as a query param in the second request URL.



The following is the response of the second request, and you can see the second element from the previous response being returned. You can also see the variable value (2) in the second request URL.



Example using a global variable as base URL of the request

In Variables section, create a variable named baseurl and assign the value https://jsonplaceholder.typicode.com/ to it.



Supply the following values for the API request.

HTTP method: GET
URL: {{$baseurl}}users/



Run the test to view its response. You can also see the variable value inserted in the request URL.



  1. Variables