How do I get JSON returned from a Playwright POST?

2.7k views Asked by At

I have created a small Playwright program in C# which submits a Url, finds a button on the returned document, selects a button on that document and 'clicks' it. If I use Fiddler I can see that the 'click' generates a POST which shows JSON is returned, but I cannot seem to find any Playwright method or routine which will allow me to return this JSON. Here is the snippet:

IPlaywright playwright = await Playwright.CreateAsync();
IBrowser browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = true
});
IBrowserContext context = await browser.NewContextAsync();
IPage page = await context.NewPageAsync();
await page.GotoAsync(some_url);
IElementHandle button = await page.QuerySelectorAsync("a[id^='date-filter-update']");
if (button != null)
{
    await button.ClickAsync();
    await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
    string response = await page.ContentAsync();    // <= returns HTML and I need JSON
}

Can anyone assist me in getting the 'response' from 'await page.ContentAsync();' to return the JSON which I can see in Fiddler?

1

There are 1 answers

0
archon On

Take a look at this. You can listen to both response and request network events in Playwright.

page.on('response', response =>
      console.log(response.json()));