跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

APIResponse ​

Added in: v1.16

APIResponse class represents responses returned by api_request_context.get() and similar methods.

python
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())
python
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"}'

Methods ​

api_response.body() ​

Added in: v1.16

Returns the buffer with response body.

Returns

  • Buffer

api_response.dispose() ​

Added in: v1.16

Disposes 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.16

An object with all the response HTTP headers associated with this response.

Returns

  • Object<string, string>

api_response.headers_array() ​

Added in: v1.16

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>

api_response.json() ​

Added in: v1.16

Returns the JSON representation of response body.

This method will throw if the response body is not parsable via JSON.parse.

Returns

  • Serializable

api_response.ok() ​

Added in: v1.16

Contains a boolean stating whether the response was successful (status in the range 200-299) or not.

Returns

  • boolean

api_response.security_details() ​

Added in: v1.61

Returns 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 | Object

api_response.server_addr() ​

Added in: v1.61

Returns 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 | Object

api_response.status() ​

Added in: v1.16

Contains the status code of the response (e.g., 200 for a success).

Returns

  • int

api_response.status_text() ​

Added in: v1.16

Contains the status text of the response (e.g. usually an "OK" for a success).

Returns

  • string

api_response.text() ​

Added in: v1.16

Returns the text representation of response body.

Returns

  • string

api_response.timing() ​

Added in: v1.62

Returns 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

  • Object

api_response.url() ​

Added in: v1.16

Contains the URL of the response.

Returns

  • string

基于 Apache 2.0 许可证发布。