Search code examples
loopbackjsstrongloop

Loopback, dump request


I'm developing rest API via Loopback framework. I want to dump incoming request when someone sends POST request to model with remote method. I want to do this even if key does not match specified name in arg, argument. So, basically print POST request in console whatever it contains, something like var_dump($_POST) in PHP, for example.

someMethod.remoteMethod(
      'pickTrans',
      {
        accepts: {arg: 'argument', type: 'string'},
        returns: {arg: 'response', type:'string'}
      }
);

Thanks.


Solution

  • You can make your remote method accept object type. This will make it accept any type of argument. Then you can use loopback's getCurrentContext() function to get the context object and parse the request object as done below.

    var loopback = require('loopback');
    module.exports = function(YourModel) {
        ...
        //remote method
        YourModel.someMethod = function(argument, cb) {
            var ctx = loopback.getCurrentContext();
            var postRequest = ctx && ctx.req;
            console.log(postRequest); //here's your POST request
            ...
            cb(null, "done");
        };
        someMethod.remoteMethod(
            'pickTrans',
            {
                accepts: {arg: 'argument', type: 'object', http: {source: 'body'}},
                http: {
                    path: '/pickTrans',
                    type: 'post'
                },
                returns: {arg: 'response', type:'string'}
            }
        );
        ...
    };
    

    Hope this solves what you were trying to achieve.