Loopback post方法调用

时间:2018-03-26 13:10:30

标签: android loopbackjs strongloop loopback

我想发送带有loopback" invokeStaticMethod"的帖子请求。 请帮我怎么做。 我想将 POST API 请求发送到以下网址:

localhost:3000 / api / user / id / unblock 使用参数 {&#34; userId&#34;,&#34; blockId&#34;} < / p>

请告诉我如何使用Loopback发送POST请求

2 个答案:

答案 0 :(得分:0)

您可以创建一个像这样的远程方法:

User.unblock = function(id, userId, blockId, callback) {
  var result;
  // TODO
  callback(null, result);
};

然后,json文件中的远程方法定义如下所示:

"unblock": {
  "accepts": [
    {
      "arg": "id",
      "type": "string",
      "required": true,
      "description": "",
      "http": {
        "source": "path"
      }
    },
    {
      "arg": "userId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    },
    {
      "arg": "blockId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    }
  ],
  "returns": [
    {
      "arg": "result",
      "type": "object",
      "root": false,
      "description": ""
    }
  ],
  "description": "",
  "http": [
    {
      "path": "/:id/unblock",
      "verb": "post"
    }
  ]
}

然后,您的远程方法将如下所示: enter image description here

您可以使用函数参数,并使用一个body参数而不是两个form参数,然后从那里读取数据,尽管我相信,如果只有两个附加参数,最好将它们分开放置。但这取决于您的方法。

答案 1 :(得分:-2)

我相信这就是你要找的......

https://loopback.io/doc/en/lb3/Adding-remote-methods-to-built-in-models.html

在你的情况下,它应该看起来像这样......

module.exports = function(app) {
  const User = app.models.User;

  User.unblock = function(userId, blockId, cb) {
      ... <Your logic goes here> ...
      cb(null, result);
  };

  User.remoteMethod('unblock', {
      accepts: [{arg: 'userId', type: 'string'}, {arg: 'blockId', type: 'string'}],
      returns: {arg: 'result', type: 'string'}
  });