跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

APIRequestContext ​

Added in: v1.16

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 instance — page.request is a shortcut for page.context().request). You can also create a standalone, isolated instance with apiRequest.newContext().

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.newContext(). Such APIRequestContext object will have its own isolated cookie storage.

Methods ​

apiRequestContext.addCookies() ​

Added in: v1.64 async

Adds cookies into this request context. They will be sent with matching subsequent requests. For browserContext.request and page.request, this is equivalent to calling browserContext.addCookies() on the corresponding browser context.

Usage

js
await request.addCookies([cookieObject1, cookieObject2]);

Arguments

  • cookies Array<Object>

    • name string

    • value string

    • url string (optional)

      Either url or both domain and path are required. Optional.

    • domain string (optional)

      For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com". Either url or both domain and path are required. Optional.

    • path string (optional)

      Either url or both domain and path are required. Optional.

    • expires float (optional)

      Unix time in seconds. Optional.

    • httpOnly boolean (optional)

      Optional.

    • secure boolean (optional)

      Optional.

    • sameSite "Strict" | "Lax" | "None" (optional)

      Optional.

    • partitionKey string (optional)

      For partitioned third-party cookies (aka CHIPS), the partition key. Optional.

apiRequestContext.clearCookies() ​

Added in: v1.64 async

Removes cookies from this request context. Accepts optional filter. For browserContext.request and page.request, this is equivalent to calling browserContext.clearCookies() on the corresponding browser context.

Usage

js
await request.clearCookies();
await request.clearCookies({ name: 'session-id' });
await request.clearCookies({ domain: 'my-origin.com' });
await request.clearCookies({ domain: /.*my-origin\.com/ });
await request.clearCookies({ path: '/api/v1' });
await request.clearCookies({ name: 'session-id', domain: 'my-origin.com' });

Arguments

  • options Object (optional)

    • domain string | RegExp (optional)

      Only removes cookies with the given domain.

    • name string | RegExp (optional)

      Only removes cookies with the given name.

    • path string | RegExp (optional)

      Only removes cookies with the given path.

apiRequestContext.cookies() ​

Added in: v1.64 async

If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs are returned. For browserContext.request and page.request, this is equivalent to calling browserContext.cookies() on the corresponding browser context.

Arguments

  • urls string | Array<string> (optional)

    Optional list of URLs.

Returns

  • Promise<Array<Object>>

apiRequestContext.delete() ​

Added in: v1.16 async

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.

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional) Added in: v1.17

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional) Added in: v1.17

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional) Added in: v1.17

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string Added in: v1.17

        File name

      • mimeType string Added in: v1.17

        File type

      • buffer Buffer Added in: v1.17

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.dispose() ​

Added in: v1.16 async

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.

Arguments

  • options Object (optional)

    • reason string (optional) Added in: v1.45

      The reason to be reported to the operations interrupted by the context disposal.

apiRequestContext.fetch() ​

Added in: v1.16 async

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:

js
await request.fetch('https://example.com/api/createBook', {
  method: 'post',
  data: {
    title: 'Book Title',
    author: 'John Doe',
  }
});

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:

js
const form = new FormData();
form.set('name', 'John');
form.append('name', 'Doe');
// Send two file fields with the same name.
form.append('file', new File(['console.log(2024);'], 'f1.js', { type: 'text/javascript' }));
form.append('file', new File(['hello'], 'f2.txt', { type: 'text/plain' }));
await request.fetch('https://example.com/api/uploadForm', {
  multipart: form
});

Arguments

  • urlOrRequest string | Request

    Target URL or Request to get all parameters from.

  • options Object (optional)

    • data string | Buffer | Serializable (optional)

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional)

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • method string (optional)

      If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional)

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string

        File name

      • mimeType string

        File type

      • buffer Buffer

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.get() ​

Added in: v1.16 async

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:

js
// Passing params as object
await request.get('https://example.com/api/getText', {
  params: {
    'isbn': '1234',
    'page': 23,
  }
});

// Passing params as URLSearchParams
const searchParams = new URLSearchParams();
searchParams.set('isbn', '1234');
searchParams.append('page', 23);
searchParams.append('page', 24);
await request.get('https://example.com/api/getText', { params: searchParams });

// Passing params as string
const queryString = 'isbn=1234&page=23&page=24';
await request.get('https://example.com/api/getText', { params: queryString });

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional) Added in: v1.26

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional) Added in: v1.26

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional) Added in: v1.26

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string Added in: v1.26

        File name

      • mimeType string Added in: v1.26

        File type

      • buffer Buffer Added in: v1.26

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.head() ​

Added in: v1.16 async

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.

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional) Added in: v1.26

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional) Added in: v1.26

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional) Added in: v1.26

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string Added in: v1.26

        File name

      • mimeType string Added in: v1.26

        File type

      • buffer Buffer Added in: v1.26

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.patch() ​

Added in: v1.16 async

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.

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional)

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional)

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional)

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string

        File name

      • mimeType string

        File type

      • buffer Buffer

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.post() ​

Added in: v1.16 async

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:

js
await request.post('https://example.com/api/createBook', {
  data: {
    title: 'Book Title',
    author: 'John Doe',
  }
});

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

js
await request.post('https://example.com/api/findBook', {
  form: {
    title: 'Book Title',
    author: 'John Doe',
  }
});

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:

js
const form = new FormData();
form.set('name', 'John');
form.append('name', 'Doe');
// Send two file fields with the same name.
form.append('file', new File(['console.log(2024);'], 'f1.js', { type: 'text/javascript' }));
form.append('file', new File(['hello'], 'f2.txt', { type: 'text/plain' }));
await request.post('https://example.com/api/uploadForm', {
  multipart: form
});

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional)

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional)

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional)

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string

        File name

      • mimeType string

        File type

      • buffer Buffer

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.put() ​

Added in: v1.16 async

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.

Arguments

  • url string

    Target URL.

  • options Object (optional)

    • data string | Buffer | Serializable (optional)

      Allows to set post data of the request. If the data parameter is an object other than a [Buffer], it will be serialized to JSON and the content-type header will be set to application/json if not explicitly set.

      If the data parameter is a string and the explicitly set content-type is application/json (case-insensitive, with optional parameters such as charset=utf-8), the string is sent as is if it parses as JSON. Otherwise, it is JSON-encoded as a string, including quotes and escaping. Strings with other content types are sent as is.

      Pass a [Buffer], such as Buffer.from(body, 'utf8'), to send exact bytes without JSON encoding, for example to send deliberately malformed JSON or a body used to compute a signature. For strings and buffers, the content-type header defaults to application/octet-stream if not explicitly set.

    • failOnStatusCode boolean (optional)

      Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.

    • form Object<string, string | float | boolean> | FormData (optional)

      Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.

    • headers Object<string, string> (optional)

      Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.

    • ignoreHTTPSErrors boolean (optional)

      Whether to ignore HTTPS errors when sending network requests. Defaults to false.

    • maxRedirects int (optional) Added in: v1.26

      Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

    • maxRetries int (optional) Added in: v1.46

      Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0 - no retries.

    • multipart FormData | Object<string, string | float | boolean | ReadStream | Object> (optional)

      Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.

      • name string

        File name

      • mimeType string

        File type

      • buffer Buffer

        File content

    • params Object<string, string | float | boolean> | URLSearchParams | string (optional)

      Query parameters to be sent with the URL.

    • signal AbortSignal (optional) Added in: v1.62

      Allows to cancel the operation using an AbortSignal. If the signal is aborted, the operation will be aborted and throw an error. Note that providing a signal does not disable the default timeout, which can be changed using browserContext.setDefaultTimeout() or page.setDefaultTimeout(); pass timeout: 0 to disable the timeout entirely.

    • timeout float (optional)

      Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Returns

  • Promise<APIResponse>

apiRequestContext.storageState() ​

Added in: v1.16 async

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

Arguments

  • options Object (optional)

    • indexedDB boolean (optional) Added in: v1.51

      Set to true to include IndexedDB in the storage state snapshot.

    • opfs boolean (optional) Added in: v1.63

      Set to true to include the origin private file system in the storage state snapshot.

    • path path (optional)

      The file path to save the storage state to. If path is a relative path, then it is resolved relative to current working directory. If no path is provided, storage state is still returned, but won't be saved to the disk.

Returns

  • Promise<Object>

Properties ​

apiRequestContext.tracing ​

Added in: v1.60

Tracing recorder for requests made through this API request context.

Type

  • Tracing

基于 Apache 2.0 许可证发布。