node.js - Loopback.io 休息连接器 - json post 不起作用

标签 node.js rest express loopbackjs

我正在尝试使用环回休息连接器代理休息服务。远程休息服务方法为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
  }

最佳答案

尝试使用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 时,会将其附加到请求 URL,即本质上以查询参数的形式发送。

关于node.js - Loopback.io 休息连接器 - json post 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544371/

相关文章:

javascript - Teamcity 上的 Angular-cli 构建(ng 构建)

javascript - 如何从 JavaScript 中的模块导出变量?

node.js - 使用 html-pdf Node 模块在标题中添加图像

Java - 将消息编码为字符串

rest - 保护移动应用程序无需登录即可访问的 REST-API

node.js - 使用 Lusca 的 hackathon-starter - csrf 值在哪里计算/存储?

javascript - 如何格式化路径以便在 Express.JS 中接收通用 JSON 对象?

node.js - Vagrant 上的 nodejs - 无法从外部加载页面

java - 我们如何使用 spring security + Oauth2 客户端凭据进行服务到服务(获取身份验证 token ,然后获取资源)

node.js - localstack AWS CloudWatch 日志