How to create an API with a query parameter that works in API explorer

19 views
Skip to first unread message

Arsalan Siddiqui

unread,
Jan 17, 2017, 10:37:07 AM1/17/17
to LoopbackJS
I want to create an API (remote method) like this, note "version" is a query paramter and not a path parameter:


I defined my remote method like this in loopback:

  elastic.insertInElastic = function (version, json, cb) {
    cb(null, insertInElastic(version, json));
  };

  elastic.remoteMethod('insertInElastic', {
    accepts: [
      {arg: 'version', type: 'string'},
      {arg: 'json', type: 'object', http: {source: 'body'}}
    ],
    returns: {arg: 'confirmation', type: 'string'},
    http: {path: '/insert', verb: 'put'}
  });
  
If I call this API through postman, it works without a problem but it fails in the loopback API explorer. What happens is that the "version" becomes undefined, meaning it doesn't get set when called through the API explorer. I am using loopback 3.2. Let me know if I am missing anything?

Erin McKean

unread,
Jan 17, 2017, 11:18:44 AM1/17/17
to LoopbackJS
I think you need to add 'http: {source: 'query'}' to your  {arg: 'version', type: 'string'}, like so: 

  elastic.remoteMethod('insertInElastic', {
    accepts: [
      {arg: 'version', type: 'string', http: {source: 'query'}},
      {arg: 'json', type: 'object', http: {source: 'body'}}
    ],
    returns: {arg: 'confirmation', type: 'string'},
    http: {path: '/insert', verb: 'put'}
  });

Arsalan Siddiqui

unread,
Jan 17, 2017, 11:42:13 AM1/17/17
to loopb...@googlegroups.com
Thank you very much Erin, it worked. Much appreciated :-)


Reply all
Reply to author
Forward
0 new messages