puppeteer-interceptor icon indicating copy to clipboard operation
puppeteer-interceptor copied to clipboard

302 redirected not response body --->throw Can only get response body on requests captured after headers received.

Open huanshiwushuang opened this issue 4 years ago • 4 comments

302 redirect not have response body,throw Error "Can only get response body on requests captured after headers received" Maybe you need a ---> try catch

try {
    const responseCdp = (await client.send('Fetch.getResponseBody', {
        requestId,
    }));
    const response = {
        body: responseCdp.base64Encoded ? atob_1.default(responseCdp.body) : responseCdp.body,
        headers: event.responseHeaders,
        errorReason: event.responseErrorReason,
        statusCode: event.responseStatusCode,
    };
    newResponse = await this.eventHandlers.onResponseReceived({ response, request });
} catch (e) {
    console.log(e);
    newResponse = await this.eventHandlers.onResponseReceived({ test: 'test' });
}

huanshiwushuang avatar Sep 11 '20 08:09 huanshiwushuang

👍 Happening to me too

JoshZA avatar Oct 14 '20 18:10 JoshZA

Happening to me too。

[2020-12-20T15:33:41.167Z] (node:69971) UnhandledPromiseRejectionWarning: Error: Protocol error (Fetch.getResponseBody): Can only get response body on requests captured after headers received.

liweiting avatar Dec 20 '20 15:12 liweiting

same to me

cqq626 avatar Jan 18 '21 07:01 cqq626

For anyone else who is looking, I think an easier way could be to just add in check for 200 responseStatusCode.

So right below the check in line 130: https://github.com/jsoverson/puppeteer-interceptor/blob/1ba312fcdfa82a329a48dd613d726a8a966e5be5/src/index.ts#L130 add something like

else if(event.responseStatusCode!=200){
        console.log(event.request.url+" => non 200 status: " + event.responseStatusCode)
}

At least in my short tests, this works great. No errors and after a 301 redirect, it just intercepts the actual url

JZL avatar Jan 27 '21 05:01 JZL