首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restify -如何发出post请求

Restify -如何发出post请求
EN

Stack Overflow用户
提问于 2018-01-11 20:15:35
回答 1查看 242关注 0票数 0

我正在尝试用Restify创建一个API,但我无法读取请求的正文。

这是代码

main.js:

代码语言:javascript
复制
'use strict';


const perfy = require('perfy');

perfy.start('startup');


const restify     = require('restify');
const operation        = require('./operation');

// post call for redirect
server.post('/test', operation.status);

operation.js:

代码语言:javascript
复制
'use strict';



module.exports =
{
    /**
     *
     * @param {Object} req  The request object as received from the middleware
     * @param {Object} res  The response object as received from the middleware
     * @param {Function} next The next function to be called by the middleware
     */
    status: function(req, res, next)
    {
        // Check configuration loading:
        CONF.get()
        .then( SETTINGS =>
        {

                req.log.debug('[TRACKER] The tracker has been requested to send the following data: %s', req.body);

                // Then return 200 if everything is OK:
                res.send(
                {
                    status: 'OK!'
                });

                next();

        })
        .catch( err =>
        {
            // Return server error:
            next(new restify.InternalError('Could not load configuration. ' + err));
        });
    }

 };

在postman中,我在POST中调用该服务:

Postman POST

然后,它接收post (返回状态: ok),但记录器不能查看正文。它未定义:

这是日志的结果:“跟踪器请求跟踪器发送以下数据:未定义”,"time":"2018-01-11T12:06:13.133Z","v":0}

我哪里做错了?

EN

回答 1

Stack Overflow用户

发布于 2018-01-12 19:58:33

使用server.use(restify.bodyParser());解决

在main.js中

代码语言:javascript
复制
'use strict';


const perfy = require('perfy');

perfy.start('startup');


const restify     = require('restify');
const operation        = require('./operation');

server.use(restify.bodyParser());

// post call for redirect
server.post('/test', operation.status);

更多信息-> RESTify on Node.js POST body / json

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48206860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档