new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · API reference · all subjects

route/methods

36 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Route.abort() aborts a route's request

Route.abort() is an async method that aborts the route's request. It accepts an optional errorCode parameter of type string.

Route.abort() error codes

Route.abort() errorCode parameter can be one of: 'aborted' (operation aborted due to user action), 'accessdenied' (permission denied), 'addressunreachable' (IP unreachable), 'blockedbyclient' (client blocked), 'blockedbyresponse' (response blocked), 'connectionaborted' (connection timeout), 'connectionclosed' (TCP FIN), 'connectionfailed' (connection attempt failed), 'connectionrefused' (refused), 'connectionreset' (TCP RST), 'internetdisconnected' (internet lost), 'namenotresolved' (host not resolved), 'timedout' (operation timeout), 'failed' (generic failure). Defaults to 'failed'.

Route.continue() sends request with optional overrides

Route.continue() is an async method that sends the route's request to the network with optional overrides. It immediately sends the request, preventing other matching handlers from being invoked. Use Route.fallback() instead if you want next matching handler to be invoked.

Route.continue() method parameter

Route.continue() accepts an optional method parameter of type string. If set, changes the request method (e.g. GET or POST).

Route.continue() postData parameter

Route.continue() accepts an optional postData parameter. In JavaScript and Python, type is string|Buffer|Serializable. In Java, type is string|Buffer. In C#, type is Buffer. If set, changes the post data of request.

Route.continue() headers parameter

Route.continue() accepts an optional headers parameter of type Object<string, string>. If set, changes the request HTTP headers. Header values will be converted to a string. Forbidden request headers (Cookie, Host, Content-Length, etc.) cannot be overridden; original headers will be used instead.

Route.continue() headers apply to redirects

The headers option in Route.continue() applies to both the routed request and any redirects it initiates. However, url, method, and postData only apply to the original request and are not carried over to redirected requests.

Route.fallback() continues with optional overrides

Route.fallback() is an async method that continues route's request with optional overrides. Similar to Route.continue() but other matching handlers will be invoked before sending the request. Allows intermediate handler to modify request before subsequent handlers process it.

Route.fallback() handler order

When multiple routes match a pattern, they run in reverse order of registration. The last registered route runs first and can override all previous ones. Other handlers are invoked in sequence with Route.fallback() until one calls Route.continue() or Route.abort().

Route.fallback() url parameter

Route.fallback() accepts an optional url parameter of type string. If set, changes the request URL. New URL must have same protocol as original one. Changing URL does not affect route matching; routes are matched using the original request URL.

Route.fallback() method parameter

Route.fallback() accepts an optional method parameter of type string. If set, changes the request method (e.g. GET or POST).

Route.fallback() postData parameter

Route.fallback() accepts an optional postData parameter. In JavaScript and Python, type is string|Buffer|Serializable. In Java, type is string|Buffer. In C#, type is Buffer. If set, changes the post data of request.

Route.fallback() headers parameter

Route.fallback() accepts an optional headers parameter of type Object<string, string>. If set, changes the request HTTP headers. Header values will be converted to a string.

Route.fetch() performs request and returns APIResponse

Route.fetch() is an async method that performs the request and fetches result without fulfilling it, so that the response could be modified and then fulfilled. Returns APIResponse.

Route.fetch() maxRedirects parameter

Route.fetch() accepts an optional maxRedirects parameter of type int. Maximum number of request redirects that will be followed automatically. An error is thrown if exceeded. Defaults to 20. Pass 0 to not follow redirects.

Route.fetch() maxRetries parameter

Route.fetch() accepts an optional maxRetries parameter of type int. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. Defaults to 0 (no retries).

Route.fetch() timeout parameter

Route.fetch() accepts an optional timeout parameter of type float. Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

Route.fetch() method parameter

Route.fetch() accepts an optional method parameter of type string. If set, changes the request method (e.g. GET or POST).

Route.fetch() postData parameter

Route.fetch() accepts an optional postData parameter. In JavaScript and Python, type is string|Buffer|Serializable. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise content-type header will be set to application/octet-stream if not explicitly set. In Java, type is string|Buffer. In C#, type is Buffer.

Route.fetch() headers parameter

Route.fetch() accepts an optional headers parameter of type Object<string, string>. If set, changes the request HTTP headers. Header values will be converted to a string. The headers option will apply to the fetched request as well as any redirects initiated by it.

Route.fulfill() fulfills request with response

Route.fulfill() is an async method that fulfills route's request with given response.

Route.fulfill() status parameter

Route.fulfill() accepts an optional status parameter of type int. Response status code, defaults to 200.

Route.fulfill() headers parameter

Route.fulfill() accepts an optional headers parameter of type Object<string, string>. Response headers. Header values will be converted to a string.

Route.fulfill() contentType parameter

Route.fulfill() accepts an optional contentType parameter of type string. If set, equals to setting Content-Type response header.

Route.fulfill() body parameter

Route.fulfill() accepts an optional body parameter. In JavaScript and Python, type is string|Buffer. In C# and Java, type is string. Response body.

Route.fulfill() bodyBytes parameter

Route.fulfill() accepts an optional bodyBytes parameter of type Buffer in C# and Java. Optional response body as raw bytes.

Route.fulfill() json parameter

Route.fulfill() accepts an optional json parameter of type Serializable in JavaScript, Python, and C#. JSON response. This method will set the content type to application/json if not set.

Route.fulfill() path parameter

Route.fulfill() accepts an optional path parameter of type path. File path to respond with. The content type will be inferred from file extension. If path is a relative path, then it is resolved relative to the current working directory.

Route.fulfill() response parameter

Route.fulfill() accepts an optional response parameter of type APIResponse. Individual fields of the response (such as headers) can be overridden using fulfill options.

Route.request() returns Request object

Route.request() is a method (not async) that returns the Request object to be routed.

Route.continue() example with header override

Example of overriding headers in Route.continue(): JavaScript: await page.route('**/*', async (route, request) => { const headers = { ...request.headers(), foo: 'foo-value', bar: undefined, }; await route.continue({ headers }); }); Python async: async def handle(route, request): headers = { **request.headers, "foo": "foo-value", "bar": None } await route.continue_(headers=headers) await page.route("**/*", handle) C#: await page.RouteAsync("**/*", async route => { var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "bar" } }; headers.Remove("origin"); await route.ContinueAsync(new() { Headers = headers }); });

Route.fetch() example with JSON modification

Example of fetching and modifying JSON response in Route.fetch(): JavaScript: await page.route('https://dog.ceo/api/breeds/list/all', async route => { const response = await route.fetch(); const json = await response.json(); json.message['big_red_dog'] = []; await route.fulfill({ response, json }); }); Python async: async def handle(route): response = await route.fetch() json = await response.json() json["message"]["big_red_dog"] = [] await route.fulfill(response=response, json=json) await page.route("https://dog.ceo/api/breeds/list/all", handle) C#: await page.RouteAsync("https://dog.ceo/api/breeds/list/all", async route => { var response = await route.FetchAsync(); dynamic json = await response.JsonAsync(); json.message.big_red_dog = new string[] {}; await route.FulfillAsync(new() { Response = response, Json = json }); });

Route.fulfill() example with 404 response

Example of fulfilling all requests with 404 responses: JavaScript: await page.route('**/*', async route => { await route.fulfill({ status: 404, contentType: 'text/plain', body: 'Not Found!' }); }); Python async: await page.route("**/*", lambda route: route.fulfill( status=404, content_type="text/plain", body="not found!")) C#: await page.RouteAsync("**/*", route => route.FulfillAsync(new () { Status = 404, ContentType = "text/plain", Body = "Not Found!" }));

Route.fulfill() example serving static file

Example of serving static file with Route.fulfill(): JavaScript: await page.route('**/xhr_endpoint', route => route.fulfill({ path: 'mock_data.json' })); Python async: await page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json")) C#: await page.RouteAsync("**/xhr_endpoint", route => route.FulfillAsync(new() { Path = "mock_data.json" }));

Route.fallback() example with handler chaining

Example of Route.fallback() with multiple handlers: JavaScript: await page.route('**/*', async route => { await route.abort(); }); await page.route('**/*', async route => { await route.fallback(); }); await page.route('**/*', async route => { await route.fallback(); }); Handlers run in reverse registration order: last registered runs first, then fallback to previous, then first registered aborts.

Route.fallback() example filtering by request method

Example of Route.fallback() filtering by request method: JavaScript: await page.route('**/*', async route => { if (route.request().method() !== 'GET') { await route.fallback(); return; } // Handling GET only. }); await page.route('**/*', async route => { if (route.request().method() !== 'POST') { await route.fallback(); return; } // Handling POST only. }); Python async: async def handle_get(route): if route.request.method != "GET": await route.fallback() return async def handle_post(route): if route.request.method != "POST": await route.fallback() return await page.route("**/*", handle_get) await page.route("**/*", handle_post)

Give your agent this brain