环回:使用 REST 连接器发送带有 json 数据的请求
Loopback: Send request with json data using REST connector
我想在我的环回应用程序中从第 3 方 REST 服务获取数据。为了从服务中获取数据,我需要先使用登录名和密码进行身份验证。我可以使用 curl 测试服务:
curl -b cookies -c cookies -X POST -d '{"auth": {"username":"MyUser","password":"Secret"}}' 'http://api.awesome.service.com/'
效果很好。 Tcpdump 显示这样的请求:
Host: http://api.awesome.service.com/
User-Agent: curl/7.47.0
Accept: */*
Cookie: HBFAPI_SESSID=hbapi%3A197887%3A58a3028d12c36%3Anym2
Content-Length: 58
Content-Type: application/x-www-form-urlencoded
{"auth":{"username":"MyUser","password":"Secret"}}
所以,我首先创建了数据源:
{
"awesome_datasource": {
"name": "awesome_datasource",
"baseURL": "https://api.awesome.service.com/",
"crud": false,
"connector": "rest",
"operations": [{
"template": {
"method": "POST",
"url": "http://api.awesome.service.com/auth",
"form":{
"auth": {
"username": "{username:string}",
"password": "{password:string}"
}
},
"json": true
},
"functions":{
"login": ["username", "password"]
}
}]
}
}
我使用资源管理器进行了测试。无论我做什么,我都无法获取格式为 json 的请求 body 中的数据。有或没有 json 选项结果都是一样的,在 tcpdump 中是:
host: api.awesome.service.com
content-type: application/x-www-form-urlencoded
accept: application/json
content-length: 66
Connection: close
auth%5Busername%5D=MyUser&auth%5Bpassword%5D=Secret
我试过将参数作为 'query'、'form' 或 'data' 选项传递。还检查了各种 header content-type 选项,但到目前为止还没有运气。
模型很简单,没有参数。基本模型是 'Model'(没有用户,因为我想尽可能简单)
我找到了这个帖子,但帮助不大:
https://github.com/strongloop/loopback-connector-rest/pull/12
任何建议将不胜感激。
那么你所要做的就是在此处使用 json
而不是 form
:
{
"awesome_datasource": {
"name": "awesome_datasource",
"baseURL": "https://api.awesome.service.com/",
"crud": false,
"connector": "rest",
"operations": [{
"template": {
"method": "POST",
"url": "http://api.awesome.service.com/auth",
"json": {
"auth": {
"username": "{username:string}",
"password": "{password:string}"
}
}
},
"functions":{
"login": ["username", "password"]
}
}]
}
}
REST 连接器尝试尽可能模仿 request module。基本上 body
、json
、form
和 query
(请求中的 qs
)与请求选项做同样的事情,但它们也接受模板字符串。这是请求的文档,稍作修改:
query
(qs
在请求中)- object 包含要附加到 uri
的查询字符串值
body
- PATCH、POST 和 PUT 请求的实体 body。必须是 Buffer
、String
或 ReadStream
。如果 json
是 true
,那么 body
必须是 JSON-serializable object.
form
- 当传递 object 或查询字符串时,这会将 body
设置为值的查询字符串表示,并添加 Content-type: application/x-www-form-urlencoded
header。当没有传递任何选项时,返回一个 FormData
实例(并通过管道传递给请求)。请参阅上面的 "Forms" 部分。
json
- 将值的 body
设置为 JSON 并添加 Content-type: application/json
header。此外,将响应 body 解析为 JSON.
我想在我的环回应用程序中从第 3 方 REST 服务获取数据。为了从服务中获取数据,我需要先使用登录名和密码进行身份验证。我可以使用 curl 测试服务:
curl -b cookies -c cookies -X POST -d '{"auth": {"username":"MyUser","password":"Secret"}}' 'http://api.awesome.service.com/'
效果很好。 Tcpdump 显示这样的请求:
Host: http://api.awesome.service.com/
User-Agent: curl/7.47.0
Accept: */*
Cookie: HBFAPI_SESSID=hbapi%3A197887%3A58a3028d12c36%3Anym2
Content-Length: 58
Content-Type: application/x-www-form-urlencoded
{"auth":{"username":"MyUser","password":"Secret"}}
所以,我首先创建了数据源:
{
"awesome_datasource": {
"name": "awesome_datasource",
"baseURL": "https://api.awesome.service.com/",
"crud": false,
"connector": "rest",
"operations": [{
"template": {
"method": "POST",
"url": "http://api.awesome.service.com/auth",
"form":{
"auth": {
"username": "{username:string}",
"password": "{password:string}"
}
},
"json": true
},
"functions":{
"login": ["username", "password"]
}
}]
}
}
我使用资源管理器进行了测试。无论我做什么,我都无法获取格式为 json 的请求 body 中的数据。有或没有 json 选项结果都是一样的,在 tcpdump 中是:
host: api.awesome.service.com
content-type: application/x-www-form-urlencoded
accept: application/json
content-length: 66
Connection: close
auth%5Busername%5D=MyUser&auth%5Bpassword%5D=Secret
我试过将参数作为 'query'、'form' 或 'data' 选项传递。还检查了各种 header content-type 选项,但到目前为止还没有运气。
模型很简单,没有参数。基本模型是 'Model'(没有用户,因为我想尽可能简单)
我找到了这个帖子,但帮助不大:
https://github.com/strongloop/loopback-connector-rest/pull/12
任何建议将不胜感激。
那么你所要做的就是在此处使用 json
而不是 form
:
{
"awesome_datasource": {
"name": "awesome_datasource",
"baseURL": "https://api.awesome.service.com/",
"crud": false,
"connector": "rest",
"operations": [{
"template": {
"method": "POST",
"url": "http://api.awesome.service.com/auth",
"json": {
"auth": {
"username": "{username:string}",
"password": "{password:string}"
}
}
},
"functions":{
"login": ["username", "password"]
}
}]
}
}
REST 连接器尝试尽可能模仿 request module。基本上 body
、json
、form
和 query
(请求中的 qs
)与请求选项做同样的事情,但它们也接受模板字符串。这是请求的文档,稍作修改:
query
(qs
在请求中)- object 包含要附加到uri
的查询字符串值
body
- PATCH、POST 和 PUT 请求的实体 body。必须是Buffer
、String
或ReadStream
。如果json
是true
,那么body
必须是 JSON-serializable object.form
- 当传递 object 或查询字符串时,这会将body
设置为值的查询字符串表示,并添加Content-type: application/x-www-form-urlencoded
header。当没有传递任何选项时,返回一个FormData
实例(并通过管道传递给请求)。请参阅上面的 "Forms" 部分。json
- 将值的body
设置为 JSON 并添加Content-type: application/json
header。此外,将响应 body 解析为 JSON.