首页 > 解决方案 > 使用esp32到aws时如何解决错误400?

问题描述

新来的。

我正在做一个项目,我试图通过 HTTPS 帖子将一些数据发布到 AWS,但似乎无法正常工作,因为我收到了 400 条消息作为响应。我已经尝试过 Postman 并且我的 API 工作正常。

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "";
const char* password = "";

//Domain name with URL path or IP address with path
const char* serverName = ""



unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED) {
      WiFiClient client;
      HTTPClient http;

      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);

      http.addHeader("Content-Type", "application/json");
      int httpResponseCode = http.POST("{\"INPUT\": \"hello world\"}");

      

      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);

      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

关于什么可能是解决方案的任何syggestions?

标签: amazon-web-servicesesp32

解决方案


对于内容类型,您需要添加“\r\n”字符:

"Content-Type: application/json\r\n"

如果需要,您可能还需要传递证书信息。有关更多详细信息,请参阅这篇文章:

esp32-https-post-json-to-aws


推荐阅读