Devz
Devz

Reputation: 603

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 ?

Upvotes: 7

Views: 5793

Answers (2)

Kishan Jayant
Kishan Jayant

Reputation: 91

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..

Upvotes: 8

kunal pareek
kunal pareek

Reputation: 1283

You can annotate every single parameter that you want separately.

for instance

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

Upvotes: 0

Related Questions