'How do I get JSON returned from a Playwright POST?

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?



Solution 1:[1]

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()));

Solution 2:[2]

Well .. I got it working in the end, despite the negligible documentation, examples or explanations which forced me into endless unnecessary tests and contortions and jumping through hoops ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 archon
Solution 2 Richard Hammond