Remote method with get parameters in url using loopback
Asked Answered
B

2

7

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 ?

Butler answered 19/4, 2017 at 20:12 Comment(0)
F
8

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

Fusil answered 28/10, 2017 at 20:54 Comment(1)
you saved my time. Thank you but we need to add something like this to get path parameter accepts: [ {arg: 'id', type: 'string', required: true, http: {source: path}}]Helmand
A
0

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'
      }
    }
  )
Academy answered 27/4, 2017 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.