Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote method with get parameters in url using loopback

i'm trying to add a get remote method to my api using loopback 2.0 in order to achieve the same method structure as the default ones, such as :

/myObject/{id}

The way I've tried is :

  MyObject.remoteMethod(
    'remotemethod', {
      http: {
        path: '/',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'string'},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

But it only allows me to do this :

http://localhost:3000/api/myObject?id=1

Does anyone knows how i can achieve this ?

Does someone also know how i can add a description to this route to display in the explorer ? The documentation doesn't really say much about this.. I think their documentation is not complete, am i the only one who feels that way ?

like image 473
Devz Avatar asked Apr 19 '17 20:04

Devz


1 Answers

Answer for loopback 3.0 (but I assume it works similar for 2.0)

MyObject.remoteMethod(
    'remotemethod', {
      description: 'This will insert the description',
      http: {
        path: '/:id',
        verb: 'get'
      },
      accepts: [
        {arg: 'id', type: 'number', required: true},
      ],
      returns: {
        arg: 'status',
        type: 'string'
      }
    }
  )

The trick is to add a required attribute to your id parameter and include the param in the path.

Also see example on how to add description

I do have to agree that the docs are quite incomplete still..

like image 85
Kishan Jayant Avatar answered Nov 02 '22 05:11

Kishan Jayant