Intercept document download with Puppeteer and Extract CSV Data

Viewed 418

I would like to download a .csv file from the browser, intercept it, and extract the data to convert it into a JSON object. Most responses say to use the requests.buffer(), however, my situation is unique as it always says the buffer is empty, but the file downloads.

I have tried to pull requests.buffer()

downloadPage.on('request', request => {
        console.log(request.isNavigationRequest());
        console.log(nextRequest);
        if (request.isNavigationRequest() && !nextRequest) {
            return request.abort();
        }
        initialRequest = false;

        request.continue();
    });

    downloadPage.on('response', async (response) => {
        console.log(response.buffer());
        file_data = JSON.parse(response.buffer());
    })


    await Promise.all([
        downloadPage.goto('https://clients.messagelabs.com/Tools/Track-And-Trace/DownloadCsv.ashx?sessionid=' + json_data.request.SessionId).catch(err => console.log(err)),
        page.waitForNavigation()
    ])

Since I am on a corporate network, it won't let me upload my images, but...

When I proceed to the link above on downloadPage.goto(...) it automatically begins a .csv file download. Than the page closes. I think the page closing is clearing the buffer, however, I can't seem to intercept the response to grab the file data before this happens. Any ideas are appreciated.

Please do not link me to another github that tells me to use the request.buffer(), as I have tried many variations.

Error: Protocol error (Network.getResponseBody): No data found for resource with given identifier

0 Answers
Related