【问题标题】:POST request with ESP32 returns 301带有 ESP32 的 POST 请求返回 301
【发布时间】:2018-11-05 16:24:35
【问题描述】:

我在树莓派上有一个 NGINX 服务器。我想将 JSON 文件从我的 ESP32 板发送到 NGINX 网络服务器。

在第一步中,我遵循了本教程: https://techtutorialsx.com/2017/05/20/esp32-http-post-requests/

它给出了这个代码:

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

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

void setup() {

  Serial.begin(115200);
  delay(4000);   //Delay needed before calling the WiFi.begin

  WiFi.begin(ssid, password); 

  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");

}

void loop() {

 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

   HTTPClient http;   

   http.begin("http://jsonplaceholder.typicode.com/posts");  //Specify destination for HTTP request
   http.addHeader("Content-Type", "text/plain");             //Specify content-type header

   int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request

   if(httpResponseCode>0){

    String response = http.getString();                       //Get the response to the request

    Serial.println(httpResponseCode);   //Print return code
    Serial.println(response);           //Print request answer

   }else{

    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);

   }

   http.end();  //Free resources

 }else{

    Serial.println("Error in WiFi connection");   

 }

  delay(10000);  //Send a request every 10 seconds

}

如果我将这段代码与以下行一起使用没有问题

http.begin("http://jsonplaceholder.typicode.com/posts");

我的 ARDUINO IDE 的串口监视器返回:

201
{
  "id": 101
}

但是,当我只用

替换上一行时
http.begin("http://82.145.56.62:151/postfile");

我在我的串行监视器上看到这个:

301
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

你知道我的问题吗?

谢谢,

编辑

我已经走得更远了。我在 posts 目录中创建了一个 php 文件。现在我得到了 HTTP 代码 200,所以我的网络服务器收到了 POST 请求。

新问题:我不知道如何在我的 php 文件中显示 POST 内容。你有什么想法吗?

谢谢,

【问题讨论】:

  • 该响应是从 nginx 服务器生成的,该服务器可能正在侦听您指出的 url。确保 nginx 配置正确。

标签: nginx post http-post esp32


【解决方案1】:

我已经通过替换解决了我的问题:

http.addHeader("Content-Type", "text/plain");

通过

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多