For more information about API Checks in general, see our API Check article.
Extract Step
An Extract Step extracts data out of JSON, XML, or HTML formatted data.
To extract data out of JSON, supply three things:
- The source containing the JSON,
- The JSONPath expression to extract out the data, and
- The name of the custom variable that you want to save to.
The source can be any JSON, but most likely will come from the response body. The source could also come from a response header or can be a custom value. The source must be well-formed JSON.
JSON Example
In the example below, we visit a site with a request step, and then extract data out of the response body. We use the JSONPath expression, $..entries[0].link
, to pull out the link from the first entry in the response. The result is stored in the custom
namespace in the variable named rigor_url
. This is accessed in the next request step as {{custom.rigor_url}}
. If you would like to test out a JSONPath expression against JSON, there is a handy JSONPath Expression Tester.
A successful extraction will result in a JSON primitive being stored into the custom variable as a string.
To extract data out of XML, supply three things:
- the source containing the XML,
- the XPath expression to extract out the data, and
- the name of the custom variable that you want to save to.
The source can be any XML, but most likely will come from the response body. The source could also come from a response header or can be a custom value. The source must be well-formed, but will not be checked for adherence to a specific schema.
XML Example
In the example below, we visit a site with a request step, and then extract data out of the response body. We use the XPath expression, //slideshow/slide[1]/title/text()
, to pull out the title from the first slide. The result is stored in the custom
namespace in the variable named title
. This is accessed in the next assert step as {{custom.title}}
.
If you would like to test out an XPath expression against XML, there are several online testers, including:
- http://www.freeformatter.com/xpath-tester.html
- http://www.xpathtester.com/xpath
- http://codebeautify.org/Xpath-Tester
HTML
To extract data out of HTML, supply three things:
- the source containing the HTML,
- the XPath expression to extract out the data, and
- the name of the custom variable that you want to save to.
The source can be any HTML, but most likely will come from the response body. The source could also come from a response header or can be a custom value. Unlike the XML extraction, the source is not required to be well-formed.
If you would like to test out an XPath expression against HTML, the easiest way is to load the file into your browser and use the developer tools to search the source. The easiest way in Chrome is to use Chrome’s built-in $x
function. In the JavaScript console supply the XPath expression as an argument to the $x
function. For example, $x('//img')
will find all of the images on the page.
HTML Example
In the example below, we visit a site with a request step, and then extract data out of the response body. We use the XPath expression, //h1/text()
, to pull out the text from the <h1>
tag. The result is stored in the custom
namespace in the variable named header_text
. This is accessed in the next assert step as {{custom.header_text}}
.
Failures
An extraction will fail if there are no results or if there are more than one result.
For JSON extractions, note that this means that the step will pass if the extraction returns a single JSON object or array. These can be operated on just like any other JSON object, which allows for pulling out an object and making assertions on it.
For XML or HTML extractions, a result can either be a string or an XML or HTML snippet. The snippet can be operated on like a full document.