H Dindi
H Dindi

Reputation: 1552

LoopBack Post API Consume req.body

I have the following model which is supposed to consume messages from a 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'
                }
            }
    );




};

However the data being posted does not come with a predefined argument rather it comes as a raw body with content_type : Application/JSON format.

How can I configure my hl7_message post consumer to get the body of the posted value ? e.g req.body

Upvotes: 0

Views: 1158

Answers (1)

user8120138
user8120138

Reputation:

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

For example, an argument getting the whole request body as the value:

{ arg: 'data', type: 'object', http: { source: 'body' } }

You would add the above line to your accepts array in the remote method description and an extra parameter (data) to the function itself.

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

Upvotes: 1

Related Questions