APIResponse
APIResponse class represents responses returned by api_request_context.get() 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"
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())
Methods
body
Added in: v1.16Returns the buffer with response body.
Usage
api_response.body()
Returns
dispose
Added in: v1.16Disposes the body of this response. If not called then the body will stay in memory until the context closes.
Usage
api_response.dispose()
Returns
json
Added in: v1.16Returns the JSON representation of response body.
This method will throw if the response body is not parsable via JSON.parse.
Usage
api_response.json()
Returns
security_details
Added in: v1.61Returns 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.
Usage
api_response.security_details()
Returns
- NoneType | Dict#
-
issuerstr (optional)Common Name component of the Issuer field. from the certificate. This should only be used for informational purposes. Optional.
-
protocolstr (optional)The specific TLS protocol used. (e.g.
TLS 1.3). Optional. -
subjectNamestr (optional)Common Name component of the Subject field from the certificate. This should only be used for informational purposes. Optional.
-
validFromfloat (optional)Unix timestamp (in seconds) specifying when this cert becomes valid. Optional.
-
validTofloat (optional)Unix timestamp (in seconds) specifying when this cert becomes invalid. Optional.
-
server_addr
Added in: v1.61Returns 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.
Usage
api_response.server_addr()
Returns
text
Added in: v1.16Returns the text representation of response body.
Usage
api_response.text()
Returns
Properties
headers
Added in: v1.16An object with all the response HTTP headers associated with this response.
Usage
api_response.headers
Returns
headers_array
Added in: v1.16An 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.
Usage
api_response.headers_array
Returns
ok
Added in: v1.16Contains a boolean stating whether the response was successful (status in the range 200-299) or not.
Usage
api_response.ok
Returns
status
Added in: v1.16Contains the status code of the response (e.g., 200 for a success).
Usage
api_response.status
Returns
status_text
Added in: v1.16Contains the status text of the response (e.g. usually an "OK" for a success).
Usage
api_response.status_text
Returns
timing
Added in: v1.62Returns 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.
Usage
api_response.timing
Returns
- Dict#
-
startTimefloatRequest start time in milliseconds elapsed since January 1, 1970 00:00:00 UTC
-
domainLookupStartfloatTime immediately before the client starts the domain name lookup for the resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
domainLookupEndfloatTime immediately after the client ends the domain name lookup for the resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
connectStartfloatTime immediately before the client starts establishing the connection to the server to retrieve the resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
secureConnectionStartfloatTime immediately before the client starts the handshake process to secure the current connection. The value is given in milliseconds relative to
startTime, -1 if not available. -
connectEndfloatTime immediately after the client establishes the connection to the server to retrieve the resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
requestStartfloatTime immediately before the client starts requesting the resource from the server, cache, or local resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
responseStartfloatTime immediately after the client receives the first byte of the response from the server, cache, or local resource. The value is given in milliseconds relative to
startTime, -1 if not available. -
responseEndfloatTime immediately after the client receives the last byte of the resource or immediately before the transport connection is closed, whichever comes first. The value is given in milliseconds relative to
startTime, -1 if not available.
-
url
Added in: v1.16Contains the URL of the response.
Usage
api_response.url
Returns