- PhantomJS Tutorial
- PhantomJS - Home
- PhantomJS - Overview
- PhantomJS - Environment Setup
- PhantomJS - Object
- PhantomJS - Methods
- WebPage Module
- PhantomJS - Properties
- PhantomJS - Methods
- PhantomJS - Events/Callbacks
- PhantomJS - Child Process Module
- File System Module
- PhantomJS - Properties
- PhantomJS - Methods
- System Module
- PhantomJS - Properties
- Web Server Module
- PhantomJS - Properties
- PhantomJS - Methods
- Miscellaneous
- Command Line Interface
- PhantomJS - Screen Capture
- PhantomJS - Page Automation
- PhantomJS - Network Monitoring
- PhantomJS - Testing
- PhantomJS - REPL
- PhantomJS - Examples
- PhantomJS Useful Resources
- PhantomJS - Quick Guide
- PhantomJS - Useful Resources
- PhantomJS - Discussion
PhantomJS - customHeaders Property
The customHeaders function specifies additional HTTP request headers that will be send to server for every request issued by the page. The default value is an empty object "{}". Header name and values are encoded in US-ASCII before being sent to the server.
Syntax
Its syntax is as follows −
var wpage = require('webpage').create(); wpage.customHeaders = { //specify the headers };
Example
The following example shows the use of customHeaders property.
var page = require('webpage').create(); var server = require('webserver').create(); var port = 8080; var listening = server.listen(8080, function (request, response) { console.log("GOT HTTP REQUEST"); console.log(JSON.stringify(request, null, 4)); }); var url = "http://localhost:" + port + "/foo/response.php"; console.log("sending request to :" +url); page.customHeaders = {'Accept-Language' : 'en-GB,en-US;q = 0.8,en;q = 0.6'}; //additional headers are mentioned here page.open(url, function (status) { if (status !== 'success') { console.log('page not opening'); } else { console.log("Getting response from the server:"); } phantom.exit(); });
Output with customheader
The above program generates the following output.
sending request to :http://localhost:8080/foo/response.php GOT HTTP REQUEST { "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q = 0.9,*/*;q = 0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-GB,en-US;q = 0.8,en;q = 0.6", "Connection": "Keep-Alive", "Host": "localhost:8080", "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1" }, "httpVersion": "1.1", "method": "GET", "url": "/foo/response.php" }
In above example, we are using customheader to set the Accept-Language and the same is shown in the http headers.
Output without customheader
The above program generates the following output without the customheader.
sending request to :http://localhost:8080/foo/response.php GOT HTTP REQUEST { "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q = 0.9,*/*;q = 0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-IN,*", "Connection": "Keep-Alive", "Host": "localhost:8080", "User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1" }, "httpVersion": "1.1", "method": "GET", "url": "/foo/response.php" }
Without the custom header, the accept language is as shown as shown in the above output. Using the customheader property, you can change the required http headers.
phantomjs_webpage_module_properties.htm
Advertisements