Rayrun
← Back to Discord Forum

Mocking APIs for POST method

caesarsaladposted in #help-playwright
Open in Discord

hi there,

let's say i have the same endpoint /project that i use for both GET and POST. but for these 2 requests, i want to mock different response. how can i use the page.route() API to mock these responses. how can it differentiate if my request if POST or GET? thanks!

This thread is trying to answer question "How can I use the `page.route()` API to mock different responses for GET and POST requests on the same endpoint?"

2 replies

Something similar is in documentation here: https://playwright.dev/docs/api/class-route You can target by route.request().method() if it is POST or GET

// Handle GET requests.
await page.route('**/*', route => {
  if (route.request().method() !== 'GET') {
    route.fallback();
    return;
  }
  // Handling GET only.
  // ...
});

// Handle POST requests.
await page.route('**/*', route => {
  if (route.request().method() !== 'POST') {
    route.fallback();
    return;
  }
  // Handling POST only.
  // ...
});

thank you!

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.