Reputation: 7629
Is there any way to request a resource with phantomjs and be able to get to the response's body?
Upvotes: 2
Views: 10751
Reputation: 1475
SlimerJS cannot work on newer version of FireFox, therefore no good for me.
This answer explains how to get the response body from XHR today in late 2019
Upvotes: 0
Reputation: 8023
Use slimmerjs
. All your 'phantomjs' code will work with 'slimmerjs' too.
More info here. Notice the body
property at the end which is only available for slimmerjs as of now.
Note: Please set page.captureContent = [/.*/]
for the 'body' to show up in the response. More info about this: here
Upvotes: 1
Reputation: 28913
UPDATE: Regarding the other possible meaning of "fetch and do something with all other resources such as images, CSS, fonts, etc.", I've recently blogged how to do this in SlimerJS. I believe the only way to do it in PhantomJS as of 1.9.1 is to apply a patch and recompile.
Maybe I'm misunderstanding what you mean by "response body", or maybe it was added to PhantomJS more recently than this question, but it is as easy as this:
var page = require('webpage').create();
var url = 'http://google.com/';
page.open(url,function(){
console.log(page.content);
phantom.exit();
});
(By the way, use page.plainText
to get it without the HTML tags.)
If you just wanted the <body>
tag contents, none of the <head>
is an alternative way that can be used to get any part of the response:
var page = require('webpage').create();
var url = 'http://google.com/';
page.open(url,function(){
var html = page.evaluate(function(){
return document.getElementsByTagName('body')[0].innerHTML;
});
console.log(html);
phantom.exit();
});
Upvotes: 4
Reputation: 3223
This is one big problem with PhantomJS right now. The open (as of writing) ticket is located at http://code.google.com/p/phantomjs/issues/detail?id=158 and as of yet, have no reliable solutions. This applies to collecting your request data as well as response data, so you cannot collect your submitted post data, then re-send with a CasperJS download like scheme.
Upvotes: 1