Skip to main content

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 an associated APIRequestContext, accessible via BrowserContext#request or Page#request (these return the

same instancepage.request is a shortcut for page.context().request). You can also create a standalone, isolated instance with APIRequest#new_context.

Cookie management

The APIRequestContext returned by BrowserContext#request and

Page#request uses the same cookie jar as its BrowserContext:

If you want API requests that do not share cookies with the browser, create an isolated context via APIRequest#new_context. Such APIRequestContext object will have its own isolated cookie storage.

playwright.chromium.launch do |browser|
# 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.
context = browser.new_context(base_url: 'https://api.github,com')
api_request_context = context.request


# Create a repository.
response = api_request_context.post(
"/user/repos",
headers: {
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer #{API_TOKEN}",
},
data: { name: 'test-repo-1' },
)
response.ok? # => true
response.json['name'] # => "test-repo-1"

# Delete a repository.
response = api_request_context.delete(
"/repos/YourName/test-repo-1",
headers: {
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer #{API_TOKEN}",
},
)
response.ok? # => true
end

delete

def delete(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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.

dispose

def dispose(reason: nil)

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

fetch

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)

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

get

def get(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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/get_text", params: query_params)
def head(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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.

patch

def patch(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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.

post

def post(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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/create_book", 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):

form_data = {
title: "Book Title",
body: "John Doe",
}
api_request_context.post("https://example.com/api/find_book", form: form_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. Use FormData to construct request body and pass it to the request as multipart parameter:

api_request_context.post(
"https://example.com/api/upload_script",
multipart: {
fileField: {
name: "f.js",
mimeType: "text/javascript",
buffer: "console.log(2022);",
},
},
)

put

def put(
url,
data: nil,
failOnStatusCode: nil,
form: nil,
headers: nil,
ignoreHTTPSErrors: nil,
maxRedirects: nil,
maxRetries: nil,
multipart: nil,
params: nil,
timeout: nil)

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.

tracing