在Vantiq平台中,我们可以通过SELECT来对外部的REST API进行访问。在SELECT访问外部数据时,我们有时候可以通过asFullResponse标识来获取更多的REST请求信息。这些信息在某些情况下非常有用。在我们Vantiq平台的tutorials其中的一个例子sources就是访问外部的一个天气的source。
我们首先可以通过平台提供的import菜单,把项目导入到Vantiq平台中:
一旦导入项目,我们可以看到如下的内容:
为了使得这个weather的source可以正常工作,我们必须在REST API接口的网站申请自己的开发者appID。
我们同时也可以定义一个如下的procedure来读取source里的数据:
PROCEDURE read_weather()
var data = SELECT FROM SOURCE weather
运行上面的代码,我们可以看到如下的结果:
[
{
"coord": {
"lon": -122.12,
"lat": 37.89
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 281.86,
"pressure": 1026,
"humidity": 75,
"temp_min": 279.15,
"temp_max": 284.26
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 120
},
"clouds": {
"all": 1
},
"dt": 1554902663,
"sys": {
"type": 1,
"id": 3581,
"message": 0.0109,
"country": "US",
"sunrise": 1554903617,
"sunset": 1554950332
},
"id": 5364226,
"name": "Lafayette",
"cod": 200
}
]
显然,这里返回的结果是我们正常的所需要的数据。但是,如果我们把上面的procedure改为:
PROCEDURE read_weather()
var data = SELECT FROM SOURCE weather WITH asFullResponse = true
在这里,我们在请求时加入了asFullResponse = true,我们再看看我们返回的信息:
[
{
"status": 200,
"headers": {
"Server": "openresty",
"Date": "Wed, 10 Apr 2019 13:30:17 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "448",
"Connection": "keep-alive",
"X-Cache-Key": "/data/2.5/weather?APPID=8f0e5fcebf2aa8ae2b3fcb3a9e9a499b&id=5364226",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET, POST"
},
"cookies": [],
"body": {
"coord": {
"lon": -122.12,
"lat": 37.89
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 281.86,
"pressure": 1026,
"humidity": 75,
"temp_min": 279.15,
"temp_max": 284.26
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 120
},
"clouds": {
"all": 1
},
"dt": 1554902663,
"sys": {
"type": 1,
"id": 3581,
"message": 0.0109,
"country": "US",
"sunrise": 1554903617,
"sunset": 1554950332
},
"id": 5364226,
"name": "Lafayette",
"cod": 200
}
}
]
我们比较一下上面返回的两种数据结果。可以看出来,加入asFullResponse = true后,多了:
- status HTTP调用返回的结果
- headers HTTP请求返回的headers。如果header有多个值,将以数组的形式返回
- cookies 返回的cookies,以数组的形式返回
- body 返回的body (正常的没有加上asFullResponse返回的结果)