A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://playwright.dev/python/docs/api/class-apirequestcontext below:

APIRequestContext | Playwright Python

APIRequestContext

This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare 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 browser_context.request or page.request. It is also possible to create a new APIRequestContext instance manually by calling api_request.new_context().

Cookie management

APIRequestContext returned by browser_context.request and 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 should create a new APIRequestContext by calling api_request.new_context(). Such APIRequestContext object will have its own isolated cookie storage.

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:



browser = p.chromium.launch()
context = browser.new_context(base_url="https://api.github.com")
api_request_context = context.request
page = context.new_page()






response = api_request_context.post(
"/user/repos",
headers={
"Accept": "application/vnd.github.v3+json",

"Authorization": f"token {API_TOKEN}",
},
data={"name": REPO},
)
assert response.ok
assert response.json()["name"] == REPO


response = api_request_context.delete(
f"/repos/{USER}/{REPO}",
headers={
"Accept": "application/vnd.github.v3+json",

"Authorization": f"token {API_TOKEN}",
},
)
assert response.ok
assert await response.body() == '{"status": "ok"}'
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):



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()





response = await api_request_context.post(
"/user/repos",
headers={
"Accept": "application/vnd.github.v3+json",

"Authorization": f"token {API_TOKEN}",
},
data={"name": REPO},
)
assert response.ok
assert response.json()["name"] == REPO


response = await api_request_context.delete(
f"/repos/{USER}/{REPO}",
headers={
"Accept": "application/vnd.github.v3+json",

"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())
Methods deleteAdded in: v1.16 apiRequestContext.delete

Sends HTTP(S) 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.

Usage

api_request_context.delete(url)
api_request_context.delete(url, **kwargs)

Arguments

Returns

disposeAdded in: v1.16 apiRequestContext.dispose

All responses returned by api_request_context.get() and similar methods are stored in the memory, so that you can later call api_response.body().This method discards all its resources, calling any method on disposed APIRequestContext will throw an exception.

Usage

api_request_context.dispose()
api_request_context.dispose(**kwargs)

Arguments

Returns

fetchAdded in: v1.16 apiRequestContext.fetch

Sends HTTP(S) 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.

Usage

JSON objects can be passed directly to the request:

data = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.fetch("https://example.com/api/createBook", method="post", data=data)

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/uploadScript", method="post",
multipart={
"fileField": {
"name": "f.js",
"mimeType": "text/javascript",
"buffer": b"console.log(2022);",
},
})

Arguments

Returns

getAdded in: v1.16 apiRequestContext.get

Sends HTTP(S) GET 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.

Usage

Request parameters can be configured with params option, they will be serialized into the URL search parameters:

query_params = {
"isbn": "1234",
"page": "23"
}
api_request_context.get("https://example.com/api/getText", params=query_params)

Arguments

Returns

headAdded in: v1.16 apiRequestContext.head

Sends HTTP(S) HEAD 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.

Usage

api_request_context.head(url)
api_request_context.head(url, **kwargs)

Arguments

Returns

patchAdded in: v1.16 apiRequestContext.patch

Sends HTTP(S) 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.

Usage

api_request_context.patch(url)
api_request_context.patch(url, **kwargs)

Arguments

Returns

postAdded in: v1.16 apiRequestContext.post

Sends HTTP(S) 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.

Usage

JSON objects can be passed directly to the request:

data = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.post("https://example.com/api/createBook", data=data)

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):

formData = {
"title": "Book Title",
"body": "John Doe",
}
api_request_context.post("https://example.com/api/findBook", form=formData)

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/uploadScript'",
multipart={
"fileField": {
"name": "f.js",
"mimeType": "text/javascript",
"buffer": b"console.log(2022);",
},
})

Arguments

Returns

putAdded in: v1.16 apiRequestContext.put

Sends HTTP(S) 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.

Usage

api_request_context.put(url)
api_request_context.put(url, **kwargs)

Arguments

Returns

storage_stateAdded in: v1.16 apiRequestContext.storage_state

Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.

Usage

api_request_context.storage_state()
api_request_context.storage_state(**kwargs)

Arguments

Returns


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4