class Playwright::APIRequestContext
“‘
assert await response.body() == ’{“status”: “ok”}‘
assert response.ok
)
},
“Authorization”: f“token {API_TOKEN}”,
# Add GitHub personal access token.
“Accept”: “application/vnd.github.v3+json”,
headers={
f“/repos/{USER}/{REPO}”,
response = api_request_context.delete(
# Delete a repository.
assert response.json() == REPO
assert response.ok
)
data={“name”: REPO},
},
“Authorization”: f“token {API_TOKEN}”,
# Add GitHub personal access token.
“Accept”: “application/vnd.github.v3+json”,
headers={
“/user/repos”,
response = api_request_context.post(
# Create a repository.
# api_request_context = p.request.new_context(base_url=“api.github.com”)
# Alternatively you can create a APIRequestContext manually without having a browser context attached:
page = context.new_page()
api_request_context = context.request
context = browser.new_context(base_url=“api.github.com”)
browser = p.chromium.launch()
# it will automatically set the cookies to the browser page and vice versa.
# requests with the internal APIRequestContext (e.g. `context.request` or `page.request`)
# This will launch a new browser, create a context and page. When making HTTP
with sync_playwright() as p:
API_TOKEN = os.getenv(“GITHUB_API_TOKEN”)
USER = “github-username”
REPO = “test-repo-1”
from playwright.sync_api import sync_playwright
import os
“`python sync
storage.
calling [`method: APIRequest.newContext`]. Such `APIRequestContext` object will have its own isolated cookie
If you want API requests to not interfere with the browser cookies you should create a new `APIRequestContext` by
this API, your e2e test will be logged in and vice versa.
`BrowserContext` cookies and requests made from the page will pick them up. This means that if you log in using
values from the browser context. If the API response contains `Set-Cookie` header it will automatically update
storage with the corresponding `BrowserContext`. Each API request will have `Cookie` header populated with the
`APIRequestContext` returned by [`property: BrowserContext.request`] and [`property: Page.request`] shares cookie
**Cookie management**
It is also possible to create a new APIRequestContext instance manually by calling [`method: APIRequest.newContext`].
the browser context and can be accessed via [`property: BrowserContext.request`] or [`property: Page.request`].
Each Playwright browser context has associated with it `APIRequestContext` instance which shares cookie storage with
environment or the service to your e2e test.
This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare
def delete(
The method will populate request cookies from the context and update
Sends HTTP(S) [DELETE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE) request and returns its response.
def delete( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.delete(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def dispose(reason: nil)
def dispose(reason: nil) wrap_impl(@impl.dispose(reason: unwrap_impl(reason))) end
def event_emitter_proxy
def event_emitter_proxy _emitter_proxy ||= EventEmitterProxy.new(self, @impl)
def fetch(
})
},
"buffer": b"console.log(2022);",
"mimeType": "text/javascript",
"name": "f.js",
"fileField": {
multipart={
"https://example.com/api/uploadScript", method="post",
api_request_context.fetch(
```python
The common way to send file(s) in the body of a request is to upload them as form fields with `multipart/form-data` encoding, by specifiying the `multipart` parameter:
```
api_request_context.fetch("https://example.com/api/createBook", method="post", data=data)
}
"body": "John Doe",
"title": "Book Title",
data = {
```python
JSON objects can be passed directly to the request:
**Usage**
context cookies from the response. The method will automatically follow redirects.
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update
def fetch( urlOrRequest, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, method: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.fetch(unwrap_impl(urlOrRequest), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), method: unwrap_impl(method), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def get(
api_request_context.get("https://example.com/api/getText", params=query_params)
}
"page": "23"
"isbn": "1234",
query_params = {
```python
Request parameters can be configured with `params` option, they will be serialized into the URL search parameters:
**Usage**
context cookies from the response. The method will automatically follow redirects.
The method will populate request cookies from the context and update
Sends HTTP(S) [GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) request and returns its response.
def get( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.get(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def head(
The method will populate request cookies from the context and update
Sends HTTP(S) [HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) request and returns its response.
def head( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.head(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def off(event, callback)
-- inherited from EventEmitter --
def off(event, callback) event_emitter_proxy.off(event, callback) end
def on(event, callback)
-- inherited from EventEmitter --
def on(event, callback) event_emitter_proxy.on(event, callback) end
def once(event, callback)
-- inherited from EventEmitter --
def once(event, callback) event_emitter_proxy.once(event, callback) end
def patch(
The method will populate request cookies from the context and update
Sends HTTP(S) [PATCH](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH) request and returns its response.
def patch( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.patch(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def post(
})
},
"buffer": b"console.log(2022);",
"mimeType": "text/javascript",
"name": "f.js",
"fileField": {
multipart={
"https://example.com/api/uploadScript'",
api_request_context.post(
```python
The common way to send file(s) in the body of a request is to upload them as form fields with `multipart/form-data` encoding. Use `FormData` to construct request body and pass it to the request as `multipart` parameter:
```
api_request_context.post("https://example.com/api/findBook", form=formData)
}
"body": "John Doe",
"title": "Book Title",
formData = {
```python
To send form data to the server use `form` option. Its value will be encoded into the request body with `application/x-www-form-urlencoded` encoding (see below how to use `multipart/form-data` form encoding to send files):
```
api_request_context.post("https://example.com/api/createBook", data=data)
}
"body": "John Doe",
"title": "Book Title",
data = {
```python
JSON objects can be passed directly to the request:
**Usage**
context cookies from the response. The method will automatically follow redirects.
The method will populate request cookies from the context and update
Sends HTTP(S) [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request and returns its response.
def post( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.post(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def put(
The method will populate request cookies from the context and update
Sends HTTP(S) [PUT](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT) request and returns its response.
def put( url, data: nil, failOnStatusCode: nil, form: nil, headers: nil, ignoreHTTPSErrors: nil, maxRedirects: nil, maxRetries: nil, multipart: nil, params: nil, timeout: nil) wrap_impl(@impl.put(unwrap_impl(url), data: unwrap_impl(data), failOnStatusCode: unwrap_impl(failOnStatusCode), form: unwrap_impl(form), headers: unwrap_impl(headers), ignoreHTTPSErrors: unwrap_impl(ignoreHTTPSErrors), maxRedirects: unwrap_impl(maxRedirects), maxRetries: unwrap_impl(maxRetries), multipart: unwrap_impl(multipart), params: unwrap_impl(params), timeout: unwrap_impl(timeout))) end
def storage_state(indexedDB: nil, path: nil)
def storage_state(indexedDB: nil, path: nil) raise NotImplementedError.new('storage_state is not implemented yet.') end