Ernesto Barbosa
Ernesto Barbosa

Reputation: 21

Loopback rest connector remote method not working

I'm using Loopback as an API interface for a 3rd party API. I'm trying to call a method of the 3rd party API through a remote method in Loopback.

Without Loopback, the way to successfully call the 3rd party method is as follows:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: */*' --header 'authorization: Bearer eyJhbGciOiJIUzUxMiJ9....' -d '{ \ 
     "attr1":"123", \ 
     "attr2":"456" \ 
     }' 'http://third-party-host/plugins/aca74a80/'

With Loopback, I have a datasource which looks like the following:

{
  ...
  "APIDataSource": {
     "name":"APIDataSource",
     "crud": false,
     "connector": "rest",
     "operations": [
        {
           "functions": {
              "saveEntityAttributes": ["entityId", "mrequest", "authkey"]
           },
           "template": {
              "method": "POST",
              "url": "http://third-party-host/plugins/{entityId}",
              "headers": {
                 "authorization":"Bearer {authkey}"
              },
              "json":"{mrequest}"
           }
        }
     ]
  }
}

and then a model code like the following:

'use strict';

module.exports = function(Model) {

Model.saveEntityAttributes = function(req, cb) {
    Model.app.models.MyAPI.saveEntityAttributes(req)
    .then(result => {
      console.log(result);
      cb(null, result);
    })
 }

Model.remoteMethod (

    'saveEntityAttributes',
    {
         http: {path: '/saveentityattributes', verb: 'post'},
         accepts: [ {arg: 'req', type: 'object', http: { source: 'req' } }],
         returns: {root: true}
    }

  );
};

This is throwing the error: "request is not json" which makes sense, since the resulting "json" content is the "entityID" parameter, instead of the intended "mrequest" parameter (which contains the json: "{"attr1":"123", "attr2":"456"}"), as shown in the loopback log:

loopback:connector:rest Request: {"method":"POST","uri":"http://third-party-host/plugins/aca74a80","json":"aca74a80","headers":{"authorization":"Bearer eyJhbGciOiJIUzUxMiJ9...."}} +0ms
loopback:connector:rest Error Response (status code: 400): "Request is not a JSON object" +152ms

My question is how can I bring the "mrequest" content to the "json" body, instead of the current "entityId" content?

Any advice is welcome. Thanks!

Upvotes: 0

Views: 663

Answers (1)

Ernesto Barbosa
Ernesto Barbosa

Reputation: 21

I solved this already!

In the datasources, the "json" option has to be replaced with: "body":"{mrequest:object}"

then, in the model code, the current 'req' http source of the remote method has to be replaced with: 'query'

Then the loopback log shows the proper request call:

loopback:connector:rest Request: {"method":"POST","uri":"http://third-party-host/plugins/aca74a80","json":true,"headers":{"authorization":"Bearer eyJhbGciOiJIUzUxMiJ9..."},"body":{"art1":"1qa","atr2":"2ws"}} +0ms

Upvotes: 1

Related Questions