1

I did some reading on this issue and I found that, at one point, what I want was possible. In comment #3, this shows up:

request.post = {
  Name : "Jonathan Doe",
  Age : "23",
  Formula : "a + b == 13%!"
}

Now, this is exactly what I want to get when I send a POST request to my PhantomJS webserver.

I'm sending it like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "localhost:8585");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
 'ajaxUrl' => $ajaxUrl,
 'analysisFile' => $analysisFile,
 'businessId' => $businessId,
 'website' => $website
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); 

..but the request I get just looks like this (after JSON.stringify):

{ "headers" : { "Accept" : "*/*",
      "Content-Length" : "555",
      "Content-Type" : "multipart/form-data; boundary=----------------------------ad33c9f28b99",
      "Expect" : "100-continue",
      "Host" : "localhost:8585"
    },
  "httpVersion" : "1.1",
  "method" : "POST",
  "post" : "------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"ajaxUrl\"\r\n\r\nhttp://localhost/website/ajax.php\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"analysisFile\"\r\n\r\nC:\\xampp\\htdocs\\website\\phantom\\get_site_info.js\r\n------------------------------ad33c9f28b99\r\nContent-Disposition: form-data; name=\"businessId\"\r\n\r\n67\r\n------------------------------d33c9f28b99\r\nContent-Disposition: form-data; name=\"website\"\r\n\r\nhttp://www.website.com/\r\n------------------------------ad33c9f28b99--\r\n",
  "url" : "/"
}

As you can see, there's no POST object, just a large string that has all of the POST data in it. Is it the way I'm sending it via cURL? I'm pretty unfamiliar with that, and the cURL code I got from here.

I'm running phantomjs 1.9.1 with casperjs 1.1.0-DEV, if that helps.

Community
  • 1
  • 1
Seiyria
  • 2,112
  • 3
  • 25
  • 51

2 Answers2

0

Just call parse and not stringify JSON.parse(yourData);

Sam Aleksov
  • 1,201
  • 6
  • 12
0

In addition to Sam Aleksovs answer (Use JSON.parse not JSON.stringify).

You need to encode the data to JSON. You can't just send the array:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
halfer
  • 19,824
  • 17
  • 99
  • 186
immulatin
  • 2,118
  • 1
  • 12
  • 13
  • Ah-ha! There we go. Encoding the data did the trick. Weirdly, `JSON.parse` freezes phantoms webserver, so I don't know about that, but it was just for debugging anyway. Thanks! – Seiyria Jul 18 '13 at 23:43