question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request interception continue method probably not working with POST method

See original GitHub issue

I’m using Puppeteer 2.0.0 (Node.js v11.12.0) and want to perform a POST request. When running the following code:

const puppeteer = require("puppeteer");
const devices = require("puppeteer/DeviceDescriptors");

async function main() {
  const browser = await puppeteer.launch({
    args: ["--enable-features=NetworkService", "--no-sandbox"],
    ignoreHTTPSErrors: true
  });
  const page = await browser.newPage();

  await page.setRequestInterception(true);

  page.on("request", interceptedRequest => {
    const method = "POST", postData = "foo=FOO&bar=BAR";
    interceptedRequest.continue({ method, postData });
  });

  const response = await page.goto("https://postman-echo.com/post");

  console.log({
    url: response.url(),
    statusCode: response.status(),
    method: response.request().method(),
    body: await response.text()
  });

  setTimeout(async () => await browser.close(), 1000);
}

main();

I get statusCode: 200 and method: ‘GET’ like it wasn’t intercepted.

What is even more confusing - when I try to do it with CDP Session:

const puppeteer = require("puppeteer");
const devices = require("puppeteer/DeviceDescriptors");

async function main() {
  const browser = await puppeteer.launch({
    args: ["--enable-features=NetworkService", "--no-sandbox"],
    ignoreHTTPSErrors: true
  });
  const page = await browser.newPage();
  const session = await page.target().createCDPSession();

  await session.send( "Network.enable" );
  await session.send( "Network.setRequestInterception", {
      patterns: [{
        urlPattern: "*",
        interceptionStage: "HeadersReceived"
      }]
    });

  session.on( "Network.requestIntercepted", async ({ interceptionId, request, responseHeaders, resourceType }) => {

    await session.send( "Network.continueInterceptedRequest", {
      interceptionId,
      url: request.url,
      method: "POST",
      postData: "foo=FOO&bar=BAR"
    });
    session.detach();
  });

  const response = await page.goto("https://postman-echo.com/post");
  console.log({
    url: response.url(),
    statusCode: response.status(),
    method: response.request().method(),
    body: await response.text()
  });

  setTimeout(async () => await browser.close(), 1000);
}

main();

I get statusCode: 404 and method: ‘GET’

Am I doing something wrong or is it an issue with Puppeteer?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dsheikocommented, Dec 4, 2019

Oh, I see

  page.on("response", async( response ) => {
    console.log("response", await response.text() );
  });

prints the server response body with the POST parameters in it. Thanks.

1reaction
kblokcommented, Dec 4, 2019

response.request contains the original request being made. request.continue won’t alter the requests array. If you test that code using https://ptsv2.com/, you will see that you are getting posts on the other side.

const response = await page.goto("https://ptsv2.com/t/wr1pa-1575457987/post");

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to intercept request in Puppeteer before current page is ...
I tried request interception but at the moment request gets intercepted the current page is already lost so I would have to go...
Read more >
Intercepting requests | Checkly
Request interception enables us to observe which requests and responses are being exchanged as part of our script's execution. For example, this is...
Read more >
Request interception with Puppeteer and Playwright
Request interception enables us to observe which requests and responses are being exchanged as part of our script's execution.
Read more >
Request Interception - Puppeteer
Once request interception is enabled, every request will stall unless it's continued, responded or aborted. An example of a naïve request interceptor that ......
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
By default, it intercepts requests matching any HTTP method. Thus when you define several intercepts, it is easy to get into the situation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found