Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Specify http method on page.route() #34253

Open
andreaslarssen opened this issue Jan 8, 2025 · 2 comments
Open

[Feature]: Specify http method on page.route() #34253

andreaslarssen opened this issue Jan 8, 2025 · 2 comments

Comments

@andreaslarssen
Copy link

🚀 Feature Request

I'm new to Playwright, coming from Cypress, so I might have missed something here.
It seems to me that when intercepting API requests with the page.route() method, you're not able to specify which http method to intercept?
If it's not possible, I would very much like to see that implemented

Example

I was expecting to see something like:

  await page.route('POST', 'my/route', (route) => {
      route.fulfill(
        { json },
      );
    });

Motivation

It would make it possible to write cleaner code.
Am I missing something here? How are we supposed to mock two different request to same URL with different http method

@Skn0tt
Copy link
Member

Skn0tt commented Jan 8, 2025

You can do this:

await page.route('my/route', (route, request) => {
  if (request.method() === 'POST')
    route.fulfill({ json })
  else
    route.continue();
});

@andreaslarssen
Copy link
Author

@Skn0tt Yes, and it's not exactly ideal. It leads to bloated boilerplate code. Seems like an easy fix to significantly raise developer friendliness?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants