完整效果图
RESTful 连接参数具体参考:POSTMAN HTTP POST 实现 RESTful Connector 连接TDengine数据库
本文参考了Node-red官网的教程 :
Set a request header
Problem
You need to send an HTTP request with specific request headers.
Solution
Set the msg.headers
field to the field value pairs of the request headers you would like to include in the message sent to the HTTP request
node.
Example
In this example we set the X-Auth-User
and X-Auth-Key
request headers to call public HttpBin post test service.
The code in the Function
node below adds these additional message fields by adding a msg.headers
object, and setting the header field/values in this object as shown.
msg.payload = "data to post";
msg.headers = {};
msg.headers['X-Auth-User'] = 'mike';
msg.headers['X-Auth-Key'] = 'fred-key';
return msg;
These can be seen by expanding the debug object headers twistie - as the test service echos back the request.
Node-red 的 单个node并不能填充完整的RESTful 连接所需的参数,所以需要使用
添加必要的参数。
具体代码:
msg.payload = "show databases;";
msg.headers = {};
msg.headers['Authorization'] = 'Basic cm9vdDp0YW9zZGF0YQ==';
return msg;
http request节点配置
如果是远程访问数据库,按域名填充
如果是本地访问数据库,按IP地址填充
完整程序:
[
{
"id": "fc190dc.51b8a7",
"type": "tab",
"label": "连接TDengine",
"disabled": false,
"info": "",
"env": []
},
{
"id": "5d67a47a.19f2cc",
"type": "inject",
"z": "fc190dc.51b8a7",
"name": "",
"props": [
{
"p": "headers.Authorization",
"v": "Basic cm9vdDp0YW9zZGF0YQ==",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 110,
"y": 120,
"wires": [
[
"73dfda3f2edaa026"
]
]
},
{
"id": "bfb93006.ddc1",
"type": "http request",
"z": "fc190dc.51b8a7",
"name": "TDengine连接请求",
"method": "POST",
"ret": "txt",
"paytoqs": "ignore",
"url": "http://xxx.armxu.top:36041/rest/sql/testdb",
"tls": "",
"persist": true,
"proxy": "",
"authType": "",
"senderr": false,
"credentials": {},
"x": 530,
"y": 120,
"wires": [
[
"60cf07ac.eab4a"
]
]
},
{
"id": "60cf07ac.eab4a",
"type": "debug",
"z": "fc190dc.51b8a7",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 120,
"wires": []
},
{
"id": "73dfda3f2edaa026",
"type": "function",
"z": "fc190dc.51b8a7",
"name": "添加请求参数",
"func": "msg.payload = \"show databases;\";\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Basic cm9vdDp0YW9zZGF0YQ==';\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 300,
"y": 120,
"wires": [
[
"bfb93006.ddc1"
]
]
},
{
"id": "f9ee71eb87d3961f",
"type": "comment",
"z": "fc190dc.51b8a7",
"name": "相关参考",
"info": "https://cookbook.nodered.org/http/set-request-header",
"x": 150,
"y": 40,
"wires": []
}
]
部署完毕后启动链接可获取服务器返回的数据:
{
"status": "succ",
"head": [
"name",
"created_time",
"ntables",
"vgroups",
"replica",
"quorum",
"days",
"keep",
"cache(MB)",
"blocks",
"minrows",
"maxrows",
"wallevel",
"fsync",
"comp",
"cachelast",
"precision",
"update",
"status"
],
"column_meta": [
[
"name",
8,
32
],
[
"created_time",
9,
8
],
[
"ntables",
4,
4
],
[
"vgroups",
4,
4
],
[
"replica",
3,
2
],
[
"quorum",
3,
2
],
[
"days",
3,
2
],
[
"keep",
8,
24
],
[
"cache(MB)",
4,
4
],
[
"blocks",
4,
4
],
[
"minrows",
4,
4
],
[
"maxrows",
4,
4
],
[
"wallevel",
2,
1
],
[
"fsync",
4,
4
],
[
"comp",
2,
1
],
[
"cachelast",
2,
1
],
[
"precision",
8,
3
],
[
"update",
2,
1
],
[
"status",
8,
10
]
],
"data": [
[
"log",
"2021-09-13 10:11:46.724",
4,
1,
1,
1,
10,
"30",
1,
3,
100,
4096,
1,
3000,
2,
0,
"us",
0,
"ready"
],
[
"testdb",
"2021-11-18 08:56:40.865",
0,
0,
1,
1,
10,
"3650",
16,
6,
100,
4096,
1,
3000,
2,
0,
"ms",
0,
"ready"
]
],
"rows": 2
}