Loopback.iorest连接器 – json发布不工作
我试图用loopback-rest-connector代理一个rest服务。 远程rest服务方法是POST,需要一个参数。 当我用查询string调用环回端点时,一切正常。 当我与身体json对象调用服务时,我得到的错误是所需的variables是未定义的,但参数是在对象ctx.req.body内。 回环没有看到它们。 我试图添加bodyparser中间件,但它也没有工作。
Datasource.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": [] }
型号configuration
"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,即基本上以查询参数的forms。