下面粘贴的是我的javascript verticle的代码,它已成功部署。由于我找不到调试js verticle的方法,我打印了routingContext的所有属性以供参考。我正在尝试获取请求的主体,但是以any
开头的所有函数都返回null。我已经在代码下面粘贴了我的请求,并在intellij终端中打印了日志。我做错了吗?
JS Verticle Code
getBody
通过ARC发出的POST请求
从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);
答案 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);