Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't add cookies in Phantomjs from JSON
    text
    copied!<p>I'm trying to load some cookies on the fly using PhantomJS, but I'm getting some errors. Here is my code:</p> <pre><code>var page = require('webpage').create(); var cookieJson = require('cookie.json'); // local cookie file phantom.cookiesEnabled = true; // Enable Cookies phantom.clearCookies(); // Clear Old Cookies for(var i = 0; i&lt; cookieJson.length; i++) { //for each domain, try to add the cookie var temp = cookieJson[i]; console.log(JSON.stringify(temp)); // This seems to print just fine phantom.addCookie(temp); // this throws an exception } phantom.exit(); </code></pre> <p>The above code throws the following exception:</p> <pre><code>incompatible type of argument(s) in call to addCookie(); candidates were addCookie(QVariantMap) </code></pre> <p>I'm sure there is a simple solution here, but I'm having a brain freeze. I'm under the impression that <code>JSON.stringify</code> returns a string from a JSON object. What's really confusing is that when I print it to console, it appears exactly the same as if I stored it as a <code>String</code> . My data looks like this:</p> <pre><code>{"domain": ".sweatytacobo.com", "expires": "Tue, 10 Jun 2014 16:37:46 GMT", "expiry": , "httponly": false, "name": "__utmz", "path": "/", "secure": false, "value": "268881515.13222266.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"} </code></pre> <p>And when I use the above as a string, it adds without a problem. So why is my <code>JSON.Stringify</code> giving me problems?</p> <p>EDIT:</p> <p>According to the comments of the <a href="https://github.com/ariya/phantomjs/blob/4989445e714bb97fe49ef8c00ccbce8b6cfbfbb0/src/phantom.h" rel="nofollow">PhantomJS source code</a> addCookie is passed a QVariantMap in the format:</p> <pre><code> { * "name" : "cookie name (string)", * "value" : "cookie value (string)", * "domain" : "cookie domain (string)", * "path" : "cookie path (string, optional)", * "httponly" : "http only cookie (boolean, optional)", * "secure" : "secure cookie (boolean, optional)", * "expires" : "expiration date (string, GMT format, optional)" * } </code></pre> <p>So shouldn't I be able to somehow pass a JSON object?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload