Data analysis data in phantomjs

I am working with the POSTMAN extension on chrome and am trying to send a request to send phantomjs. I managed to send an email request to the phantomjs script server, installing the postman, as in the attached screenshotenter image description here

My script phantoms are as follows:

// import the webserver module, and create a server
var server = require('webserver').create();
var port = require('system').env.PORT || 7788;     

console.log("Start Application");
console.log("Listen port " + port);    

// Create serever and listen port 
server.listen(port, function(request, response) {    

      console.log("request method: ", request.method);  // request.method POST or GET     

      if(request.method == 'POST' ){
                       console.log("POST params should be next: ");
                       console.log(request.headers);
                    code = response.statusCode = 200;
                    response.write(code);
                    response.close();

                }
 });  

when I run phantomjs on the command line, here is the output:

$ phantomjs.exe myscript.js
Start Application
Listen port 7788
null
request method:  POST
POST params should be next:
[object Object]
POST params:  1=bill&2=dave

So it works. My question is how to parse the message body into variables, so I can access it in the rest of the script.

+4
source share
1 answer

You should not use to read these messages, request.headersas these are HTTP headers (encoding, cache, cookies, ...)

, request.post request.postRaw.

request.post - json-, . [object Object]. JSON.stringify(request.post) .

request.post json, ( , )

script

// import the webserver module, and create a server
var server = require('webserver').create();
var port = require('system').env.PORT || 7788;

console.log("Start Application");
console.log("Listen port " + port);

// Create serever and listen port 
server.listen(port, function (request, response) {

    console.log("request method: ", request.method);  // request.method POST or GET     

    if (request.method == 'POST') {
        console.log("POST params should be next: ");
        console.log(JSON.stringify(request.post));//dump
        console.log(request.post['1']);//key is '1'
        console.log(request.post['2']);//key is '2'
        code = response.statusCode = 200;
        response.write(code);
        response.close();
    }
});
+7

All Articles