javascript - Why is Hapi.js POST handler returning an empty payload? - Stack Overflow

I have a Hapi route that accepts a POST call, but the request returns a null value for the payload.serv

I have a Hapi route that accepts a POST call, but the request returns a null value for the payload.

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

I'm using Postman to send a POST call to http://localhost:8000/create_note?name=test.

In the handler function, console.log(request.payload) returns null.

Am I doing something wrong?

I have a Hapi route that accepts a POST call, but the request returns a null value for the payload.

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

I'm using Postman to send a POST call to http://localhost:8000/create_note?name=test.

In the handler function, console.log(request.payload) returns null.

Am I doing something wrong?

Share Improve this question edited May 2, 2016 at 21:16 simon-p-r 3,7512 gold badges22 silver badges37 bronze badges asked May 2, 2016 at 14:18 parkerageeparkeragee 3231 gold badge3 silver badges14 bronze badges 1
  • payload is the request body, unless you're sending a request body through Postman, null is the expected value. – Matt Harrison Commented May 2, 2016 at 16:38
Add a ment  | 

1 Answer 1

Reset to default 7

You are passing query string parameters with ?name=test, not the POST request payload.

You can access the query parameters by referencing request.query.

An HTTP request to http://localhost:8000/create_note?name=test will yield:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745552853a4632657.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信