Skip to content

Reading API responses

Target page: https://slensky.com/zendriver-examples/api-request.html

In this tutorial, we will demonstrate how to read a dynamically loaded API response using response expectations.

The example page simulates an API request by waiting for a few seconds and then fetching a static JSON file. While it would be far easier in this case to just fetch the JSON file directly, for demonstration purposes, let's instead pretend that the response comes from a more complex API that cannot easily be called directly.

Initial setup

Begin by creating a new script for the tutorial:

import asyncio

import zendriver as zd


async def main() -> None:
    browser = await zd.start()

    # TODO: Read the API response
    page = await browser.get(
        "https://slensky.com/zendriver-examples/api-request.html",
    )

    await browser.stop()


if __name__ == "__main__":
    asyncio.run(main())

Reading the API response

import asyncio
import json

import zendriver as zd
from zendriver.cdp.network import get_response_body


async def main() -> None:
    browser = await zd.start()

    page = browser.tabs[0]
    async with page.expect_response(".*/user-data.json") as response_expectation:
        await page.get(
            "https://slensky.com/zendriver-examples/api-request.html",
        )
        response = await response_expectation.value

    request_id = response.request_id
    body, _ = await page.send(get_response_body(request_id=request_id))
    user_data = json.loads(body)

    print("Successfully read user data response for user:", user_data["name"])
    print(json.dumps(user_data, indent=2))

    await browser.stop()


if __name__ == "__main__":
    asyncio.run(main())