Search code examples
c#puppeteerpuppeteer-sharp

Evaluate Fetch Api in PuppeteerSharp


What is the c# alternative of the java code below?

await page.evaluate(() => {
  return fetch('url', {method: 'POST', body: 'test' });
});

I need to send a post request using PuppeteerSharp. There are several ways but it seems it is the easiest one, But I did not find any alternative for the fetch method.

for the evaluate method I found this method await page.EvaluateFunctionAsync for C#, but How can I use fetch inside?


Solution

  • In Puppeteer, the function that we provide to page.evaluate is executed in context of the page and so it has to be a JavaScript function.

    Likewise, in the equivalent functions in puppeteer-sharp e.g. page.EvaluateFunctionAsync, the first argument is a string which is nothing but a JavaScript function. You should be able to use the fetch as it is, something like:

    await page.EvaluateFunctionAsync(@"() => {
      return fetch('url', {method: 'POST', body: 'test' });
    }");