如何用libcurl发布包括换行符在内的数据(对于Influxdb)?

问题描述 投票:1回答:1

我正在使用libcurl将数据点发布到InfluxDB中。libcurl 在C++中。单个数据点可以工作,但是对于多个点,我在必要的换行(\n)上遇到了困难,就像定义在 InfluxDB HTTP API:

用一个请求将几个点写入数据库,每个点用一个新行分开。

用@标志从文件中写入点。该文件应包含一批行协议格式的点。单个点必须在自己的行上,并且用换行符(\n)隔开。包含回车的文件会导致解析器错误。

我想这可能是char格式的问题,但我想不通为什么?下面的代码只是写了5的距离,但应该写两个距离。

#include "stdafx.h"
#include "tchar.h"
#include <string>
#include <curl\curl.h>
#include <sstream>

int main()
{
    int dataPoints[] = { 3, 5 };
    std::string fieldIdentifier = "distance";

    std::stringstream ss;

    for (int i = 0; i < 1; i++) {
        ss << "aufbau1,ort=halle ";
        ss << fieldIdentifier;
        ss << "=" << dataPoints[i];
        ss << std::endl; //I guess this is the problem, it adds \n
    }

    ss << "aufbau1,ort=halle ";
    ss << fieldIdentifier;
    ss << "=" << dataPoints[1];

    std::string data = ss.str();
    const char *dataChar = data.c_str();
    
    CURL *curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://myIP:8086/write?db=testdb&u=myUser&p=myPwd");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataChar);
        res = curl_easy_perform(curl);

        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        curl_easy_cleanup(curl);
    }
    return 0;
}
c++ string libcurl line-breaks influxdb
1个回答
0
投票

在python中,有一个 <br> 当在Grafana中查看这些数据时,tagfield值内的html标签似乎就像一个换行符。

关于在Grafana中查看这些数据时 \n influxdb说

注意:行协议不支持标签值或字段值中的换行符\n。

https:/docs.influxdata.cominfluxdbv1.8write_protocolsline_protocol_reference。

© www.soinside.com 2019 - 2024. All rights reserved.