APIResponse
APIResponse class represents responses returned by api_request_context.get(url, **kwargs) and similar methods.
- Sync
- Async
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
context = playwright.request.new_context()
response = context.get("https://example.com/user/repos")
assert response.ok
assert response.status == 200
assert response.headers["content-type"] == "application/json; charset=utf-8"
assert response.json()["name"] == "foobar"
assert response.body() == '{"status": "ok"}'
import asyncio
from playwright.async_api import async_playwright, Playwright
async def run(playwright: Playwright):
context = await playwright.request.new_context()
response = await context.get("https://example.com/user/repos")
assert response.ok
assert response.status == 200
assert response.headers["content-type"] == "application/json; charset=utf-8"
assert response.json()["name"] == "foobar"
assert await response.body() == '{"status": "ok"}'
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
- api_response.body()
- api_response.dispose()
- api_response.headers
- api_response.headers_array
- api_response.json()
- api_response.ok
- api_response.status
- api_response.status_text
- api_response.text()
- api_response.url
api_response.body()
Added in: v1.16Returns the buffer with response body.
api_response.dispose()
Added in: v1.16Disposes the body of this response. If not called then the body will stay in memory until the context closes.
api_response.headers
Added in: v1.16An object with all the response HTTP headers associated with this response.
api_response.headers_array
Added in: v1.16An array with all the request HTTP headers associated with this response. Header names are not lower-cased. Headers with multiple entries, such as Set-Cookie
, appear in the array multiple times.
api_response.json()
Added in: v1.16- returns:Serializable># <
Returns the JSON representation of response body.
This method will throw if the response body is not parsable via JSON.parse
.
api_response.ok
Added in: v1.16Contains a boolean stating whether the response was successful (status in the range 200-299) or not.
api_response.status
Added in: v1.16Contains the status code of the response (e.g., 200 for a success).
api_response.status_text
Added in: v1.16Contains the status text of the response (e.g. usually an "OK" for a success).
api_response.text()
Added in: v1.16Returns the text representation of response body.
api_response.url
Added in: v1.16Contains the URL of the response.