2021-10-19 07:38:27 -07:00
# class: APIRequestContext
2021-11-15 14:54:07 -08:00
* langs: js, java, python
2021-09-13 12:43:07 -07:00
2021-10-05 17:53:19 -08:00
This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare
2022-03-21 13:20:17 -07:00
environment or the service to your e2e test.
Each Playwright browser context has associated with it [APIRequestContext] instance which shares cookie storage with
the browser context and can be accessed via [`property: BrowserContext.request` ] or [`property: Page.request` ].
It is also possible to create a new APIRequestContext instance manually by calling [`method: APIRequest.newContext` ].
**Cookie management**
[APIRequestContext] retuned by [`property: BrowserContext.request` ] and [`property: Page.request` ] shares cookie
storage with the corresponding [BrowserContext]. Each API request will have `Cookie` header populated with the
values from the browser context. If the API response contains `Set-Cookie` header it will automatically update
[BrowserContext] cookies and requests made from the page will pick them up. This means that if you log in using
this API, your e2e test will be logged in and vice versa.
If you want API requests to not interfere with the browser cookies you shoud create a new [APIRequestContext] by
calling [`method: APIRequest.newContext` ]. Such `APIRequestContext` object will have its own isolated cookie
storage.
2021-09-13 12:43:07 -07:00
2021-11-24 21:55:03 +01:00
```python async
import os
import asyncio
from playwright.async_api import async_playwright, Playwright
REPO = "test-repo-1"
USER = "github-username"
API_TOKEN = os.getenv("GITHUB_API_TOKEN")
async def run(playwright: Playwright):
# This will launch a new browser, create a context and page. When making HTTP
# requests with the internal APIRequestContext (e.g. `context.request` or `page.request` )
# it will automatically set the cookies to the browser page and vise versa.
browser = await playwright.chromium.launch()
context = await browser.new_context(base_url="https://api.github.com")
api_request_context = context.request
page = await context.new_page()
# Alternatively you can create a APIRequestContext manually without having a browser context attached:
# api_request_context = await playwright.request.new_context(base_url="https://api.github.com")
# Create a repository.
response = await api_request_context.post(
"/user/repos",
headers={
"Accept": "application/vnd.github.v3+json",
# Add GitHub personal access token.
"Authorization": f"token {API_TOKEN}",
},
data={"name": REPO},
)
assert response.ok
assert response.json()["name"] == REPO
# Delete a repository.
response = await api_request_context.delete(
f"/repos/{USER}/{REPO}",
headers={
"Accept": "application/vnd.github.v3+json",
# Add GitHub personal access token.
"Authorization": f"token {API_TOKEN}",
},
)
assert response.ok
assert await response.body() == '{"status": "ok"}'
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
```
```python sync
import os
from playwright.sync_api import sync_playwright
REPO = "test-repo-1"
USER = "github-username"
API_TOKEN = os.getenv("GITHUB_API_TOKEN")
with sync_playwright() as p:
# This will launch a new browser, create a context and page. When making HTTP
# requests with the internal APIRequestContext (e.g. `context.request` or `page.request` )
# it will automatically set the cookies to the browser page and vise versa.
2022-02-04 17:36:30 +02:00
browser = p.chromium.launch()
2021-11-24 21:55:03 +01:00
context = browser.new_context(base_url="https://api.github.com")
api_request_context = context.request
page = context.new_page()
# Alternatively you can create a APIRequestContext manually without having a browser context attached:
2022-02-04 17:36:30 +02:00
# api_request_context = p.request.new_context(base_url="https://api.github.com")
2021-11-24 21:55:03 +01:00
# Create a repository.
response = api_request_context.post(
"/user/repos",
headers={
"Accept": "application/vnd.github.v3+json",
# Add GitHub personal access token.
"Authorization": f"token {API_TOKEN}",
},
data={"name": REPO},
)
assert response.ok
assert response.json()["name"] == REPO
# Delete a repository.
response = api_request_context.delete(
f"/repos/{USER}/{REPO}",
headers={
"Accept": "application/vnd.github.v3+json",
# Add GitHub personal access token.
"Authorization": f"token {API_TOKEN}",
},
)
assert response.ok
assert await response.body() == '{"status": "ok"}'
```
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.delete
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [DELETE ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE ) request and returns its response.
The method will populate request cookies from the context and update
context cookies from the response. The method will automatically follow redirects.
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.delete.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.delete.params = %%-java-fetch-params-%%
### option: APIRequestContext.delete.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.delete.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.delete.data = %%-js-python-fetch-option-data-%%
### option: APIRequestContext.delete.form = %%-js-python-fetch-option-form-%%
2021-11-15 14:54:07 -08:00
### option: APIRequestContext.delete.multipart = %%-js-pyhton-fetch-option-multipart-%%
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.delete.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.delete.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.delete.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-10-07 12:42:26 -07:00
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.dispose
2021-09-15 14:02:55 -07:00
2021-10-19 11:17:23 -07:00
All responses returned by [`method: APIRequestContext.get` ] and similar methods are stored in the memory, so that you can later call [`method: APIResponse.body` ]. This method
discards all stored responses, and makes [`method: APIResponse.body` ] throw "Response disposed" error.
2021-09-15 14:02:55 -07:00
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.fetch
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 12:43:07 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update
2021-09-13 12:43:07 -07:00
context cookies from the response. The method will automatically follow redirects.
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.fetch.urlOrRequest
2021-09-13 12:43:07 -07:00
- `urlOrRequest` < [string]|[Request]>
2021-10-07 12:42:26 -07:00
Target URL or Request to get all parameters from.
2021-09-13 12:43:07 -07:00
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.fetch.params = %%-java-fetch-params-%%
### option: APIRequestContext.fetch.params = %%-js-python-fetch-option-params-%%
2021-09-13 14:29:44 -07:00
2021-10-19 07:38:27 -07:00
### option: APIRequestContext.fetch.method
2021-11-19 16:40:35 -08:00
* langs: js, python
2021-09-13 12:43:07 -07:00
- `method` < [string]>
2021-10-07 12:42:26 -07:00
If set changes the fetch method (e.g. [PUT ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT ) or
[POST ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST )). If not specified, GET method is used.
2021-09-13 12:43:07 -07:00
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.fetch.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.fetch.data = %%-js-python-fetch-option-data-%%
### option: APIRequestContext.fetch.form = %%-js-python-fetch-option-form-%%
2021-11-15 14:54:07 -08:00
### option: APIRequestContext.fetch.multipart = %%-js-pyhton-fetch-option-multipart-%%
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.fetch.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.fetch.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.fetch.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-10-05 17:53:19 -08:00
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.get
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 12:43:07 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [GET ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET ) request and returns its response.
The method will populate request cookies from the context and update
2021-09-13 12:43:07 -07:00
context cookies from the response. The method will automatically follow redirects.
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.get.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.get.params = %%-java-fetch-params-%%
### option: APIRequestContext.get.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.get.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.get.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.get.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.get.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-09-28 15:33:36 -07:00
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.head
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 12:43:07 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [HEAD ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD ) request and returns its response.
The method will populate request cookies from the context and update
2021-09-13 12:43:07 -07:00
context cookies from the response. The method will automatically follow redirects.
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.head.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.head.params = %%-java-fetch-params-%%
### option: APIRequestContext.head.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.head.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.head.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.head.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.head.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-09-13 12:43:07 -07:00
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.patch
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 12:43:07 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [PATCH ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH ) request and returns its response.
The method will populate request cookies from the context and update
context cookies from the response. The method will automatically follow redirects.
2021-09-13 14:29:44 -07:00
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.patch.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.patch.params = %%-java-fetch-params-%%
### option: APIRequestContext.patch.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.patch.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.patch.data = %%-js-python-fetch-option-data-%%
### option: APIRequestContext.patch.form = %%-js-python-fetch-option-form-%%
2021-11-15 14:54:07 -08:00
### option: APIRequestContext.patch.multipart = %%-js-pyhton-fetch-option-multipart-%%
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.patch.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.patch.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.patch.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.post
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 12:43:07 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [POST ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST ) request and returns its response.
The method will populate request cookies from the context and update
context cookies from the response. The method will automatically follow redirects.
2021-09-13 12:43:07 -07:00
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.post.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.post.params = %%-java-fetch-params-%%
### option: APIRequestContext.post.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.post.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.post.data = %%-js-python-fetch-option-data-%%
### option: APIRequestContext.post.form = %%-js-python-fetch-option-form-%%
2021-11-15 14:54:07 -08:00
### option: APIRequestContext.post.multipart = %%-js-pyhton-fetch-option-multipart-%%
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.post.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.post.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.post.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.put
2021-10-19 11:17:23 -07:00
- returns: < [APIResponse]>
2021-09-13 15:38:27 -07:00
2021-10-07 12:42:26 -07:00
Sends HTTP(S) [PUT ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT ) request and returns its response.
The method will populate request cookies from the context and update
context cookies from the response. The method will automatically follow redirects.
2021-09-28 15:33:36 -07:00
2021-10-19 07:38:27 -07:00
### param: APIRequestContext.put.url = %%-fetch-param-url-%%
2021-11-19 16:40:35 -08:00
### param: APIRequestContext.put.params = %%-java-fetch-params-%%
### option: APIRequestContext.put.params = %%-js-python-fetch-option-params-%%
### option: APIRequestContext.put.headers = %%-js-python-fetch-option-headers-%%
### option: APIRequestContext.put.data = %%-js-python-fetch-option-data-%%
### option: APIRequestContext.put.form = %%-js-python-fetch-option-form-%%
2021-11-15 14:54:07 -08:00
### option: APIRequestContext.put.multipart = %%-js-pyhton-fetch-option-multipart-%%
2021-11-19 16:40:35 -08:00
### option: APIRequestContext.put.timeout = %%-js-python-fetch-option-timeout-%%
### option: APIRequestContext.put.failOnStatusCode = %%-js-python-fetch-option-failonstatuscode-%%
### option: APIRequestContext.put.ignoreHTTPSErrors = %%-js-python-fetch-option-ignorehttpserrors-%%
2021-10-19 07:38:27 -07:00
## async method: APIRequestContext.storageState
2021-09-30 14:14:29 -07:00
- returns: < [Object]>
- `cookies` < [Array]< [Object]>>
- `name` < [string]>
- `value` < [string]>
- `domain` < [string]>
- `path` < [string]>
- `expires` < [float]> Unix time in seconds.
- `httpOnly` < [boolean]>
- `secure` < [boolean]>
- `sameSite` < [SameSiteAttribute]< "Strict"|"Lax"|"None">>
- `origins` < [Array]< [Object]>>
- `origin` < [string]>
- `localStorage` < [Array]< [Object]>>
- `name` < [string]>
- `value` < [string]>
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
2021-11-15 14:54:07 -08:00
## async method: APIRequestContext.storageState
* langs: java
- returns: < [string]>
2021-10-19 07:38:27 -07:00
### option: APIRequestContext.storageState.path = %%-storagestate-option-path-%%