【问题标题】:400 Bad Request while posting to InfluxDB with NativeEthernet使用 NativeEthernet 发布到 InfluxDB 时出现 400 错误请求
【发布时间】:2021-11-22 06:03:06
【问题描述】:

对于一个项目,我们试图监控通过 TCP 在本地网络上发送的状态协议,并通过 HTTPS 将这些状态消息发布到 AWS 上的 InfluxDB 实例https://www.influxdata.com/products/influxdb-cloud/

我们正在使用带有以太网套件的 teensy 4.1 与 vjmuzik https://github.com/vjmuzik/NativeEthernet 的本机以太网库。

通过 LAN 将字符串发布到测试实例到 IP 工作正常,但每当我们尝试发布到 AWS 子域时,都会收到以下响应:

HTTP/1.1 400 Bad Request
Server: awselb/2.0
Date: Mon, 27 Sep 2021 18:13:02 GMT
Content-Type: text/html
Content-Length: 122
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
</body>
</html>

由于我们在本地发布时验证了帖子的标题和正文是正确的,并且在使用 Postman 时可以发布到 AWS,所以我们目前不知所措。不幸的是,我没有关于我们尝试 POST 到的服务器配置的信息。

我们已经尝试过:

  • 使用不同的以太网库,目前我们正在使用 来自 vjmuzik https://github.com/vjmuzik/NativeEthernet 的 NativeEthernet
  • 不同的标题集
  • 在 ethernet.begin() 中添加 google dns
  • 通过 http 发帖,然后在 https 上发帖 由纯 html 请求生成的重定向响应
  • 添加证书
  • 使用 postman 验证 InfluxDB 线路协议

作为测试,我们使用我们自己的 POST 标头和正文设置了 WebClientRepeatingTLS 示例。

#include <NativeEthernet.h>

uint8_t mac[6];
void teensyMAC(uint8_t *mac) {
  for (uint8_t by = 0; by < 2; by++) mac[by] = (HW_OCOTP_MAC1 >> ((1 - by) * 8)) & 0xFF;
  for (uint8_t by = 0; by < 4; by++) mac[by + 2] = (HW_OCOTP_MAC0 >> ((3 - by) * 8)) & 0xFF;
  Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

// initialize the library instance:
EthernetClient client;
const int port = 443;

char server[] = "influxdata.com";  

//IPAddress server(192, 168, 1, 246);

String outputMSG = "measurementName,tagKey=tagValue fieldKey=1i";
unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 5 * 1000; // delay between updates, in milliseconds

void setup() {
  teensyMAC(mac);
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1);
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  delay(1000);
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

void httpRequest() {
  client.stop();
  Serial.print("Content-Length: ");
  Serial.println(outputMSG.length());
  Serial.println("");

  if (client.connect(server, port, true)) { 
    Serial.println("connecting to ");
    Serial.print(server);
    Serial.print(":");
    Serial.println(port);

        client.println("POST /api/v2/write?org=ORG_HERE&bucket=BUCKET_HERE&precision=s HTTP/1.1");
        client.println("Host: https://eu-central-1-1.aws.cloud2.influxdata.com");
        client.println("Authorization: Token <<TOKEN HERE>>");
        client.println("Content-Type: text/plain");
        client.print("Content-Length: ");
        client.println(outputMSG.length());
        client.println("Accept-Encoding: gzip, deflate, br");
        client.println("Connection: keep-alive");
        client.println();
        client.println(outputMSG);
        client.println();
        
    lastConnectionTime = millis();
  } else {
    Serial.println("connection failed");
  }
}

谁能帮助我们理解为什么发到 aws 的帖子没有通过?

干杯, 男孩

【问题讨论】:

  • 从主机头中删除 https://
  • 感谢您的回复!我过去曾尝试过,但得到以下响应:HTTP/1.1 301 永久移动服务器:awselb/2.0 日期:星期六,2021 年 10 月 2 日 09:54:02 GMT 内容类型:文本/html 内容长度:0 连接:关闭位置:influxdata.com/api/v2/write 之后我尝试按照 301 发出新请求,不幸的是,这仍然导致原始的 400 错误请求响应。
  • 主机头应该只包含主机名。 301向前迈了一步,400向后退了一步
  • 我明白了。你会碰巧知道从那时起如何进行吗?
  • 为什么您的主机字段与用于连接的主机名不同?

标签: https arduino influxdb ethernet teensy


【解决方案1】:

EthernetWebServerSSL 库与QNEthernet 库结合使用,可以让青少年连接到AWS 子域,从而允许它发布到Influxdb 数据库。

【讨论】:

    猜你喜欢
    • 2019-05-03
    • 2020-06-30
    • 2018-06-30
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多