在phantomjs中解析发布数据 [英] Parsing post data in phantomjs

查看:83
本文介绍了在phantomjs中解析发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用chrome的POSTMAN扩展,并尝试向phantomjs发送发帖请求.我已通过将postman设置为所附的屏幕快照,从而设法将发帖请求发送给phantomjs服务器脚本.

I am working with the POSTMAN extension to chrome and am trying to send a post request to phantomjs I have managed to send a post request to a phantomjs server script by setting postman as in the attached screenshot

我的phantomjs脚本如下:

My phantomjs script is 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();

                }
 });  

当我在命令行上运行phantomjs时,输出如下:

when I run phantomjs at 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 does appear to work. My question is now how to parse post body into variables, so I can access it in the rest of the script.

推荐答案

要读取帖子数据,请勿使用request.headers,因为它是HTTP标头(编码,缓存,cookie等)

To read post data, you should not use request.headers as it's HTTP headers (encoding, cache, cookies, ...)

此处所述,您应该使用request.postrequest.postRaw.

As said here, you should use request.post or request.postRaw.

request.post是json对象,因此您将其写入控制台.这就是为什么您得到[object Object]的原因.尝试在登录时应用JSON.stringify(request.post).

request.post is a json object, so you write it into the console. That's why you get [object Object]. Try to apply a JSON.stringify(request.post) when logging.

由于request.post是json对象,因此您也可以使用索引器直接读取属性(如果属性未发布,请不要忘记添加基本检查)

As request.post is a json object, you can also directly read properties using indexers (do not forget to add a basic check if the property is not posted)

这是脚本的更新版本

// 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();
    }
});

这篇关于在phantomjs中解析发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆