PhantomJS - onResourceReceived()



This callback is called when the resource requested by the page is received. It contains response as the argument.

The response object has the following details −

  • Id − number of the requested resource.

  • URL − The requested URL.

  • Time − The date object containing date of the response.

  • Headers − http headers.

  • BodySize − The size of the received content decompressed.

  • ContentType − The content type if specified.

  • RedirectURL − If there is redirection then the redirected URL.

  • Stage − The values are – start and end.

  • Status − The http code status i.e. status 200.

  • StatusText − The http status text for code 200, it is OK.

Syntax

Its syntax is as follows −

page.onResourceReceived = function(response) {}

Example

The following code shows the use of onResourceReceived() method.

var wpage = require('webpage').create(); 
wpage.onResourceReceived = function(response) { 
   console.log(JSON.stringify(response));   
} 
wpage.open('http://localhost/tasks/prompt.html', function(status) {  
}); 

The above program generates the following output.

{"body":"","bodySize":231,"contentType":"text/html","headers":[{"name":"Date",
"value":"Sun, 07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17 
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":"Sun, 
07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e754eee893517e5\""},
{"name":"Accept-Ranges","value":"bytes"},{"name":"ContentLength","value":"231"},
{"name":"Keep-Alive","value":"timeout=5, max=100"},{"name":"Connection","value":
"Keep-Alive"},{"name":"ContentType","value":"text/html"}],"id":1,"redirectURL":null,
"stage":"start","status": 200,"statusText":"OK","time":"2017-05-07T12:59:17.440Z",
"url": "http://localhost/tasks/prompt.html"} 

{"contentType":"text/html","headers":[{"name":"Date","value":"Sun, 
07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17 
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":
"Sun, 07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e7-54eee893517e5\""},
{"name":"AcceptRanges","value":"bytes"},{"name":"Content-Length","value":"231"},
{"name":"KeepAlive","value":"timeout=5, max=100"},{"name":"Connection",
"value":"KeepAlive"},{"name":"Content-Type", "value":"text/html"}],
"id":1,"redirectURL":null,"stage":"end","status":200,"statusText":"OK",
"time":"2017-0507T12:59:17.486Z","url":"http://localhost/tasks/prompt.html"}
phantomjs_webpage_module_events_callbacks.htm
Advertisements