获取post请求vert.x javascript verticle的正文

时间:2017-11-08 16:33:55

标签: javascript rest post vert.x

下面粘贴的是我的javascript verticle的代码,它已成功部署。由于我找不到调试js verticle的方法,我打印了routingContext的所有属性以供参考。我正在尝试获取请求的主体,但是以any开头的所有函数都返回null。我已经在代码下面粘贴了我的请求,并在intellij终端中打印了日志。我做错了吗?

JS Verticle Code

getBody

通过ARC发出的POST请求

enter image description here

从Intellij登录

var Router = require("vertx-web-js/router");
var server = vertx.createHttpServer();
var router = Router.router(vertx);
function writeHelp(obj) {
    var txt = "";
    for (var x in obj){
        txt += x + "->" + obj[x] + "\n";
    }
    return txt;
}
router.post("/provider").handler(function (routingContext) {
    var response = routingContext.response();
    var json = routingContext.getBodyAsString();
    response.putHeader("content-type", "text/plain");
    //console.log("Helper\n" + writeHelp(routingContext));
    //console.log("Request=" + writeHelp(routingContext.request()));
    console.log("body=" + json);
    response.end("Success");
});
server.requestHandler(router.accept).listen(8890);

1 个答案:

答案 0 :(得分:1)

您必须添加BodyHandler才能获得已解析的正文:

来自官方文件:

var BodyHandler = require("vertx-web-js/body_handler");

// This body handler will be called for all routes
router.route().handler(BodyHandler.create().handle);