为什么Hapi.js POST处理程序返回空负载

Why is Hapi.js POST handler returning an empty payload?

本文关键字:返回 负载 程序 处理 Hapi js POST 为什么      更新时间:2023-09-26

我有一个接受POST调用的Hapi路由,但request返回有效负载的null值。

server.route({
    method: ['POST', 'PUT'],
    path: '/create_note',
    handler: function (request, reply) {
        console.log(request.payload); // returns `null`
        return reply(request.payload);
    }
});

我正在使用Postman向http://localhost:8000/create_note?name=test发送POST调用。

在处理程序函数中,console.log(request.payload)返回null

我做错什么了吗?

您传递的是带有?name=test的查询字符串参数,而不是POST请求负载。

您可以通过引用request.query来访问查询参数。

http://localhost:8000/create_note?name=test的HTTP请求将产生:

console.log(request.query); // {name: 'test'}