【问题标题】:Loopback.io rest connector - json post not workingLoopback.io 休息连接器 - json 帖子不起作用
【发布时间】:2016-01-30 20:15:18
【问题描述】:

我正在尝试使用 loopback-rest-connector 代理休息服务。远程休息服务方式为 POST,需要一个参数。当我使用查询字符串调用环回端点时,一切正常。当我使用 body json 对象调用服务时,我收到所需变量未定义但参数位于对象 ctx.req.body 内的错误。 Loopback 看不到它们。我尝试添加 bodyparser 中间件,但也没有用。

数据源.js

{
      "db": {
        "name": "db",
        "connector": "memory",
        "file": "db.json"
      },

      "rest": {
        "name": "rest",
        "connector": "rest"
      },
      "geoRest": {
            "connector": "rest",
            "debug": "true",
            "operations": [{

                "template": {
                    "method": "POST",
                    "url": "https://url/endpoint",
                    "headers": {
                        "accept": "application/json",
                        "content-type": "application/json",
                        "Authorization": "sdfsdf"
                    },
                    "body": {
                        "address": "{^address:string}",
                        "country": "{country:string}"
                    }
                },
                "functions": {
                    "geocode": ["address", "country"]
                }
            }]
        }
    }

模型定义

{
  "name": "geoRest",
  "plural": "geoRests",
  "base": "Model",
  "strict": true,
  "idInjection": false,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

模型配置

"geoRest": {
    "dataSource": "geoRest",
    "public": true
  }

【问题讨论】:

标签: node.js rest express loopbackjs


【解决方案1】:

尝试使用form 而不是body。因此修改后的template 部分应如下所示:

"template": {
    "method": "POST",
    "url": "https://url/endpoint",
    "headers": {
        "accept": "application/json",
        "content-type": "application/json",
        "Authorization": "sdfsdf"
    },
    "form": {
        "address": "{^address:string}",
        "country": "{country:string}"
    }
},

使用form 将发送您的参数,就像使用POST 方法提交表单一样,因此作为请求正文的一部分。

在使用bodyreq 时,会将其附加到请求网址,即基本上以查询参数的形式发送。

【讨论】:

  • 带有 Post API 的环回 4 控制器怎么样?如何读取正文参数,即使用哪个注释。例如。对于路径参数,我们使用 @param.path.string
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多