English · Python
Appearance
English · Python
Appearance
APIResponse class represents responses returned by api_request_context.get() and similar methods.
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"
json_data = await response.json()
assert json_data["name"] == "foobar"
assert await response.body() == '{"status": "ok"}'
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())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"}'Returns the buffer with response body.
Returns
BufferDisposes the body of this response. If not called then the body will stay in memory until the context closes.
An object with all the response HTTP headers associated with this response.
Returns
Object<string, string>An array with all the response 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.
Returns
Array<Object>Returns the JSON representation of response body.
This method will throw if the response body is not parsable via JSON.parse.
Returns
SerializableContains a boolean stating whether the response was successful (status in the range 200-299) or not.
Returns
booleanReturns SSL and other security information. Resolves to null for non-HTTPS responses. For redirected requests, returns the information for the last request in the redirect chain.
Returns
null | ObjectReturns the IP address and port of the server. Resolves to null if the server address is not available. For redirected requests, returns the information for the last request in the redirect chain.
Returns
null | ObjectContains the status code of the response (e.g., 200 for a success).
Returns
intContains the status text of the response (e.g. usually an "OK" for a success).
Returns
stringReturns the text representation of response body.
Returns
stringReturns resource timing information for given response. For redirected requests, returns the information for the last request in the redirect chain. When the response is served from the HAR file, timing information is not available and all the values are -1. Find more information at Resource Timing API.
Returns
ObjectContains the URL of the response.
Returns
string