【问题标题】:LoopBack Post API Consume req.bodyLoopBack Post API 使用 req.body
【发布时间】:2018-03-18 01:24:13
【问题描述】:

我有以下模型,它应该使用来自 post 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'
                }
            }
    );




};

但是,发布的数据没有预定义的参数,而是作为原始正文提供,内容类型为:应用程序/JSON 格式。

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

【问题讨论】:

    标签: node.js loopbackjs loopback


    【解决方案1】:

    https://loopback.io/doc/en/lb3/Remote-methods.html#argument-descriptions

    例如,获取整个请求正文作为值的参数:

    { 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'
                }
            }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多