javascript - How to pass customHeaders as system arguments in PhantomJS script? -
i have started working in small proof of concept using phantomjs take screenshots , i'll necessary configurations system arguments such url, timeout, isscreenshotreqd, isharfilereqd, isheadersreqd, username, password , application related configs. working fine except customheaders
.
code used
if (system.args.length === 1) { console.log('usage: phantom.js <some url>'); phantom.exit(1); } else { assembleid = system.args[2]; page.address = system.args[3]; page.settings.resourcetimeout = system.args[4]; isscreenshotreqd = system.args[5]; isheadersreqd = system.args[6]; isharfilereqd = system.args[7]; page.settings.username = system.args[8]; page.settings.password = system.args[9]; var key = "headerkey";//(or system.args[10]) var value = "headervalue";//(or system.args[11]) page.customheaders = {key : value}; //some operation }
this sets customheader
"headers": [{"name": "key","value": "headervalue"}]
you can see value set correctly key not taken initialized variable or system.args[x]
instead takes whatever variable use.
though works if hardcode customheaders like
page.customheaders = {"headerkey": "headervalue"};
gives expected output problem i'll having dynamic headers various urls. means it's config driven , each customer give different headers each url.
javascript not permit use of variables object keys. have set variable key in way:
var key = "some dynamic key"; var value = "some value" var obj = {}; obj[key] = value;
the additional problem phantomjs' customheaders
needs set whole. phantomjs doesn't notice properties of customheaders
object have changed. can use this:
var key = "headerkey"; var value = "headervalue"; var customheaders = {}; customheaders[key] = value; page.customheaders = customheaders;
Comments
Post a Comment