Is there any way to request a resource with phantomjs and be able to get to the response's body?
4 Answers
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();
});

- 27,837
- 13
- 117
- 217
-
1I think it's to do with getting access to ajax responses or javascript-loaded images etc. That's certainly what brought me here. – Richard Huxton Nov 05 '13 at 21:52
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.

- 3,153
- 1
- 19
- 22
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

- 7,900
- 2
- 37
- 34
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

- 1,445
- 1
- 15
- 26