LoopBack Post API使用req.body

时间:2018-03-17 04:29:42

标签: node.js loopbackjs loopback

我有以下模型,它应该使用来自后API的消息:

'use strict';

module.exports = function (Message) {


    Message.hl7_message = function (cb) {


        cb(null, 'Success... ');
    }

    Message.remoteMethod(
            'hl7_message', {
                http: {
                    path: '/hl7_message',
                    verb: 'post',
                    status: 200,
                    errorStatus: 400
                },
                accepts: [],
                returns: {
                    arg: 'status',
                    type: 'string'
                }
            }
    );




};

然而,发布的数据没有带有预定义的参数,而是带有content_type:Application / JSON格式的原始主体。

如何配置我的hl7_message帖子消费者以获取已发布值的正文? 例如req.body

1 个答案:

答案 0 :(得分:1)

this

  

例如,一个参数将整个请求体作为值:

     

{arg:'data',输入:'object',http:{source:'body'}}

您可以将以上行添加到远程方法描述中的accepts数组中,并将额外参数(data)添加到函数本身。

Message.hl7_message = function (data, cb) {
    console.log('my request body: ' + JSON.stringify(data));
    cb(null, 'Success... ');
}

Message.remoteMethod(
        'hl7_message', {
            http: {
                path: '/hl7_message',
                verb: 'post',
                status: 200,
                errorStatus: 400
            },
            accepts: [{ arg: 'data', type: 'object', http: { source: 'body' } },
            returns: {
                arg: 'status',
                type: 'string'
            }
        }
);
相关问题