首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LoopBack Post API消费req.body

LoopBack Post API消费req.body
EN

Stack Overflow用户
提问于 2018-03-17 04:29:42
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
'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 post使用者以获取发布值的主体?例如req.body

EN

回答 1

Stack Overflow用户

发布于 2018-03-17 17:24:13

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

例如,将整个请求体作为值的参数: { arg:'data',键入:'object',http:{ source:'body‘}

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

代码语言:javascript
复制
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'
            }
        }
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49332510

复制
相关文章

相似问题

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