Skip to main content

APIRequest

提供用于 Web API 测试的接口。本类可用于创建 APIRequestContext,后者用于发送 HTTP 请求。可通过 playwright.request 获取该实例,详情参见 APIRequestContext 文档。

api_request.new_context(**kwargs)

Added in: v1.16
  • base_url <str> 作为请求的基础 URL,内部通过 URL() 组装最终地址。例如:#
    • baseURL = http://localhost:3000,请求 /bar.htmlhttp://localhost:3000/bar.html
    • baseURL = http://localhost:3000/foo/,请求 ./bar.htmlhttp://localhost:3000/foo/bar.html
    • baseURL = http://localhost:3000/foo(无尾斜杠),请求 ./bar.htmlhttp://localhost:3000/bar.html
  • extra_http_headers <Dict[str, str]> 每个请求都会附带的自定义 HTTP 头。#
  • http_credentials <Dict> HTTP 认证 所需的 username/password#
  • ignore_https_errors <bool> 是否忽略 HTTPS 错误,默认 false#
  • proxy <Dict> 代理配置:#
    • server <str> 代理地址,支持 HTTP/SOCKS,如 http://myproxy.com:3128socks5://myproxy.com:3128,简写 myproxy.com:3128 等同 HTTP 代理。
    • bypass <str> 可选,逗号分隔的免代理域名,例如 ".com, chromium.org, .domain.com"
    • username/password <str> 可选,用于需要认证的代理。
  • storage_state <Union[str, pathlib.Path]|Dict> 使用指定会话状态初始化,可来自 browser_context.storage_state(**kwargs)api_request_context.storage_state(**kwargs)#
  • timeout <float> 等待响应的最大毫秒数,默认 30000,可设为 0 表示无限。#
  • user_agent <str> 在该上下文中使用的 UA。#
  • 返回值: <APIRequestContext>#

创建新的 APIRequestContext 实例。