【发布时间】: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
}
【问题讨论】:
-
是的,我做到了。问题是,连接器不使用 body json 对象。它仅适用于查询字符串。但我需要用一个 body 对象来做请求。
标签: node.js rest express loopbackjs