Node-red http request请求实现RESTful 连接TDengine 数据库

本文介绍如何使用Node-RED通过RESTful API连接TDengine数据库,并展示具体的配置步骤与代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 完整效果图

 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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

armcsdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值