我正在尝试使用环回休息连接器代理休息服务。远程休息服务方法为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
方法提交表单一样,因此作为请求正文的一部分。
当使用 body
或 req
时,会将其附加到请求 URL,即本质上以查询参数的形式发送。
关于node.js - Loopback.io 休息连接器 - json post 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544371/