pageRequest() for API calls, and the download helpers for file downloads.
pageRequest()
Executes a fetch() call inside the browser context via page.evaluate(). Because the request runs inside the real browser process, it uses the browser’s TLS fingerprint, cookies, and session — the same as what the site’s own JavaScript would send. This avoids bot-detection issues that arise when making the same request from Node.js directly.
z.infer<T> when a schema is provided, or any otherwise.
Non-2xx responses cause
pageRequest to throw an error. The error message includes the HTTP method, URL, and status code.RequestConfig
The URL to fetch. Relative URLs are resolved in the browser context.
HTTP method. Defaults to
"GET".Additional request headers to merge with any that
bodyType adds automatically.Request body. When
bodyType is "json" (the default), objects are serialized with JSON.stringify. When bodyType is "form", objects are encoded as application/x-www-form-urlencoded.How to serialize the
body. Defaults to "json". Setting this to "form" also sets Content-Type: application/x-www-form-urlencoded automatically.How to parse the response. Defaults to
"json". Both "text" and "xml" return the raw response text.PageRequestOptions<T>
Optional logger. When provided, request details (method, URL, status, duration) are logged at
info level, and failures at warn.Optional Zod schema. When provided, the parsed response body is validated with
schema.parse() before being returned. The return type of pageRequest becomes z.infer<T>.Example
downloadViaClick()
Triggers a file download by clicking a DOM element and intercepts the resulting download using Playwright’s download event. The download promise is registered before the click so the event is never missed.
Parameters
The Playwright
Page to operate on.CSS selector for the element that triggers the download when clicked.
DownloadResult
The raw file contents.
The filename suggested by the server via the
Content-Disposition header or the URL.Example
downloadAndSave()
Convenience wrapper around downloadViaClick() that also writes the downloaded file to disk.
Parameters
The Playwright
Page to operate on.CSS selector for the element that triggers the download when clicked.
Return value
ReturnsDownloadResult & { savedTo: string } — the same buffer and filename as downloadViaClick, plus:
The absolute path the file was written to.