Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free
Home / Effective Strategies / Tips for Extracting API Response Data in Apidog

Tips for Extracting API Response Data in Apidog

Apidog provides built-in tools to extract both response headers and response body content for analysis. In this post, we’ll explore techniques for extracting API response data in Apidog.

When testing APIs in Apidog, it's often necessary to extract specific values from the API response data for further processing and usage (usually to make additional requests). Typically, the API response data is in JSON or XML format. JSON is the most common and preferred format since it's concise, readable, and easy to parse. Therefore, this article will focus on techniques for parsing JSON data.

The main parts of the response that often need processing are the response headers and the response body. Apidog provides built-in tools to extract both response headers and response body content for analysis. In this post, we’ll explore techniques for extracting API response data in Apidog.

Extracting Response Headers in Apidog

All HTTP responses contain headers that provide metadata about the response. The response headers are displayed in a list format along with their values. You can scroll through the list to inspect them.

Often, you may want to extract a specific header value for validation. For example, to test an authentication API, you may want to assert that it returns a valid token in the response.

Apidog will automatically create a variable storing that header's value. You can then reference this variable in subsequent test steps to validate the value. This provides a convenient way to pull out response headers for verification.

Sample response headers from an API:

To extract the entire value of the header with the key "Session-Token", you can simply enter the key name in the "Post Action" section for the API.

However, what if you want to extract only a portion of the Session-Token header value, such as just the msToken value? In this case, you'll need to use a script with a regular expression to perform the extraction.

To extract the Token value from Session-Token, the required script would be:

pm.response.headers.toJSON()[2]['value'].toString().match(/msToken=([^;\s"]+)/)[1]

In this script, we first get the full Session-Token header value using pm.response.headers. Then we use a regular expression to extract just the msToken value from Session-Token. This returns an array, so we index into the array to get the actual value.

Extracting Response Body Content

In addition to headers, the response body contains the payload returned by the API. To analyze the response content, you first need to extract or parse it.

If the response is JSON data, Apidog can automatically convert it into structured JSON variables. To enable this:

  1. In the "Post Processors" section for the API, select "Extract Variable".

2. Fill in the JSONPath expression and other options.

3. After setting it up, send the request. Apidog will extract the corresponding field values from the response data based on the JSONPath expression you provided. The extracted values will be automatically stored in the "Environment/Global/Temporary" variables.

Apidog will parse the JSON and create variables for the data that you can reference in later test steps.

For non-JSON content like XML or plain text, you may need to extract specific portions using regular expressions. Apidog supports visually building regex patterns to match and extract substrings or values in the response.

In Summary

Being able to extract response data is critical for effective API testing. Apidog provides flexible built-in tools for extracting both headers and response body content. Automated JSON extraction handles common use cases while regex gives you full control for complex responses.

Put these data extraction techniques to work in your API tests to simplify validation and improve coverage. Apidog lets you turn response data into actionable variables to assert key details and power follow-on test steps. More robust tests lead to higher-quality APIs and better user experiences.

Join Apidog's Newsletter

Subscribe to stay updated and receive the latest viewpoints anytime.

Please enter a valid email
Network error, please try again later
Thank you for subscribing!