APIResponse
APIResponse 类表示 api_request_context.get() 和类似方法返回的响应。
¥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.16返回带有响应正文的缓冲区。
¥Returns the buffer with response body.
用法
¥Usage
api_response.body()
返回
¥Returns
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.
用法
¥Usage
api_response.dispose()
返回
¥Returns
json
Added in: v1.16返回响应正文的 JSON 表示形式。
¥Returns the JSON representation of response body.
如果响应正文无法通过 JSON.parse
进行解析,则此方法将抛出异常。
¥This method will throw if the response body is not parsable via JSON.parse
.
用法
¥Usage
api_response.json()
返回
¥Returns
text
Added in: v1.16返回响应正文的文本表示形式。
¥Returns the text representation of response body.
用法
¥Usage
api_response.text()
返回
¥Returns
属性
¥Properties
headers
Added in: v1.16包含与此响应关联的所有响应 HTTP 标头的对象。
¥An object with all the response HTTP headers associated with this response.
用法
¥Usage
api_response.headers
返回
¥Returns
headers_array
Added in: v1.16包含与此响应关联的所有响应 HTTP 标头的数组。标头名称不是小写的。具有多个条目的标头(例如 Set-Cookie
)会多次出现在数组中。
¥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.
用法
¥Usage
api_response.headers_array
返回
¥Returns
标头的名称。
¥Name of the header.
value
str
标头的值。
¥Value of the header.
ok
Added in: v1.16包含一个布尔值,说明响应是否成功(状态范围为 200-299)。
¥Contains 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.16包含响应的状态代码(例如,200 表示成功)。
¥Contains the status code of the response (e.g., 200 for a success).
用法
¥Usage
api_response.status
返回
¥Returns
status_text
Added in: v1.16包含响应的状态文本(例如,通常 "OK" 表示成功)。
¥Contains the status text of the response (e.g. usually an "OK" for a success).
用法
¥Usage
api_response.status_text
返回
¥Returns
url
Added in: v1.16包含响应的 URL。
¥Contains the URL of the response.
用法
¥Usage
api_response.url
返回
¥Returns