-
Notifications
You must be signed in to change notification settings - Fork 9.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I get the response size of a request?except by Content-Length #3372
Comments
Via 'use strict';
const puppeteer = require('puppeteer');
(async function main() {
try {
const browser = await puppeteer.launch();
const [page] = await browser.pages();
const response1 = await page.goto('https://example.org/');
console.log(
response1.headers()['content-length'],
(await response1.buffer()).length
);
page.setExtraHTTPHeaders({ 'accept-encoding': 'identity'});
const response2 = await page.goto('https://example.org/');
console.log(
response2.headers()['content-length'],
(await response2.buffer()).length
);
await browser.close();
} catch (err) {
console.error(err);
}
})();
|
No, I mean, can I get the size of each response body, like the Google console, regardless of whether the Content-Length field is in the response header returned by the back end, because the gzip-compressed response header has no Content-Length field |
chunked response has no Content-Length header . response in CDP has encodedDataLength but puppeteer don't save it. |
Another way is to use CDP. // Monitor using CDP
const devToolsResponses = new Map();
const devTools = await page.target().createCDPSession();
await devTools.send("Network.enable");
devTools.on("Network.responseReceived", event => {
devToolsResponses.set(event.requestId, event.response);
});
devTools.on("Network.loadingFinished", event => {
const response = devToolsResponses.get(event.requestId);
const encodedBodyLength =
event.encodedDataLength - response.headersText.length;
console.log(`${encodedBodyLength} bytes for ${response.url}`);
}); However be careful using this. Such questions are more suitable for Stackoverflow since it's not an issue/feature request actually, here is a similar question. |
currently i'm using the code blew to hook into puppeteer to add the puppeteer version i use is 1.8.0 const {NetworkManager, Response} = require('puppeteer/lib/NetworkManager');
NetworkManager.prototype._onResponseReceived = function (event) {
const request = this._requestIdToRequest.get(event.requestId);
// FileUpload sends a response without a matching request.
if (!request)
return;
const response = new Response(this._client, request, event.response);
response.rawResponse = event.response; // <<<<<<<<< this is what i add
request._response = response;
this.emit(NetworkManager.Events.Response, response);
};
NetworkManager.prototype._handleRequestRedirect = function (request, responsePayload) {
const response = new Response(this._client, request, responsePayload);
response.rawResponse = responsePayload; // <<<<<<<<< this is what i add
request._response = response;
request._redirectChain.push(request);
response._bodyLoadedPromiseFulfill.call(null, new Error('Response body is unavailable for redirect responses'));
this._requestIdToRequest.delete(request._requestId);
this._attemptedAuthentications.delete(request._interceptionId);
this.emit(NetworkManager.Events.Response, response);
this.emit(NetworkManager.Events.RequestFinished, request);
}; after then, when you get a response from puppeteer, you can use |
@MythRen Very interesting approach. |
More to add to this problem, here is a target url. URL: The answers at #3372 (comment) and #3372 (comment), seems to not work at all when Is this a upstream bug @aslushnikov ? |
We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days. |
We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it! |
My approach was to edit Puppeteer's code directly with Patchdiff --git a/node_modules/puppeteer-core/lib/cjs/puppeteer/common/HTTPResponse.js b/node_modules/puppeteer-core/lib/cjs/puppeteer/common/HTTPResponse.js
index b7ce222..e29119c 100644
--- a/node_modules/puppeteer-core/lib/cjs/puppeteer/common/HTTPResponse.js
+++ b/node_modules/puppeteer-core/lib/cjs/puppeteer/common/HTTPResponse.js
@@ -57,6 +57,8 @@ class HTTPResponse extends HTTPResponse_js_1.HTTPResponse {
? new SecurityDetails_js_1.SecurityDetails(responsePayload.securityDetails)
: null, "f");
__classPrivateFieldSet(this, _HTTPResponse_timing, responsePayload.timing || null, "f");
+ this._responsePayload = responsePayload;
+ this._extraInfo = extraInfo;
}
_resolveBody(err) {
if (err) {
diff --git a/node_modules/puppeteer-core/lib/esm/puppeteer/common/HTTPResponse.js b/node_modules/puppeteer-core/lib/esm/puppeteer/common/HTTPResponse.js
index cd455ca..919b97b 100644
--- a/node_modules/puppeteer-core/lib/esm/puppeteer/common/HTTPResponse.js
+++ b/node_modules/puppeteer-core/lib/esm/puppeteer/common/HTTPResponse.js
@@ -54,6 +54,8 @@ export class HTTPResponse extends BaseHTTPResponse {
? new SecurityDetails(responsePayload.securityDetails)
: null, "f");
__classPrivateFieldSet(this, _HTTPResponse_timing, responsePayload.timing || null, "f");
+ this._responsePayload = responsePayload;
+ this._extraInfo = extraInfo;
}
_resolveBody(err) {
if (err) {
Typings// filename: puppeteer.d.ts
import {} from 'puppeteer';
interface IHTTPResponseAdditions {
_responsePayload: {
encodedDataLength: number;
};
}
declare module 'puppeteer' {
interface HTTPResponse extends IHTTPResponseAdditions {}
}
declare module 'puppeteer-core' {
interface HTTPResponse extends IHTTPResponseAdditions {}
} Updated HTTPResponseThere is now a _responsePayload property as well as an _extraInfos one:
|
Steps to reproduce
Tell us about your environment:
What steps will reproduce the problem?
Please include code that reproduces the issue.
What is the expected result?
What happens instead?
The text was updated successfully, but these errors were encountered: