Skip to main content
The Route class represents a route handler that can intercept, modify, or mock network requests. Routes are created using page.route() or context.route().

Methods

request

Returns the request associated with this route.
const request = route.request();
console.log(request.url());
Returns: Request

abort

Aborts the request.
await route.abort();
await route.abort('failed');
errorCode
string
Optional error code. Defaults to ‘failed’.Possible values: aborted, accessdenied, addressunreachable, blockedbyclient, blockedbyresponse, connectionaborted, connectionclosed, connectionfailed, connectionrefused, connectionreset, internetdisconnected, namenotresolved, timedout, failed

fulfill

Fulfills the request with a custom response.
await route.fulfill({
  status: 200,
  contentType: 'application/json',
  body: JSON.stringify({ success: true })
});
options.status
number
Response status code. Defaults to 200.
options.headers
Object
Response headers. Header values must be strings.
options.contentType
string
Response content type. If not set, will be inferred from body.
options.body
string | Buffer
Response body.
options.json
any
JSON response body. Mutually exclusive with body.
options.path
string
File path to read response body from.
options.response
APIResponse
APIResponse to fulfill with. Status and headers will be copied from the response.

continue

Continues the request with optional modifications.
await route.continue();
await route.continue({
  headers: {
    ...route.request().headers(),
    'Authorization': 'Bearer token'
  }
});
options.url
string
Override request URL.
options.method
string
Override request method (GET, POST, etc.).
options.headers
Object
Override request headers.
options.postData
string | Buffer | object
Override request post data.

fallback

Falls back to the next route handler or default behavior.
await route.fallback();
await route.fallback({
  headers: {
    ...route.request().headers(),
    'X-Custom': 'value'
  }
});
options.url
string
Override request URL.
options.method
string
Override request method.
options.headers
Object
Override request headers.
options.postData
string | Buffer | object
Override request post data.

fetch

Fetches the original request and returns the response.
const response = await route.fetch();
const body = await response.text();
await route.fulfill({ body: body.replace('old', 'new') });
options.url
string
Override request URL.
options.method
string
Override request method.
options.headers
Object
Override request headers.
options.postData
string | Buffer | object
Override request post data.
options.maxRedirects
number
Maximum number of redirects to follow. Defaults to 20.
options.maxRetries
number
Maximum number of retries. Defaults to 0.
options.timeout
number
Request timeout in milliseconds.
Returns: Promise<APIResponse>

Example Usage

Mock API Response

await page.route('**/api/users', route => {
  route.fulfill({
    status: 200,
    contentType: 'application/json',
    body: JSON.stringify([
      { id: 1, name: 'John' },
      { id: 2, name: 'Jane' }
    ])
  });
});

Block Images

await page.route('**/*.{png,jpg,jpeg}', route => route.abort());

Modify Request Headers

await page.route('**/api/**', route => {
  const headers = route.request().headers();
  route.continue({
    headers: {
      ...headers,
      'Authorization': 'Bearer my-token'
    }
  });
});

Modify Response

await page.route('**/api/data', async route => {
  const response = await route.fetch();
  const json = await response.json();
  json.modified = true;
  await route.fulfill({
    response,
    json
  });
});

Conditional Routing

await page.route('**/api/**', route => {
  if (route.request().method() === 'POST') {
    route.continue();
  } else {
    route.abort();
  }
});

Build docs developers (and LLMs) love