POST-запрос с ESP32 возвращает 301 - PullRequest
0 голосов
/ 05 ноября 2018

У меня есть сервер NGINX на Raspberry Pi. Я хочу отправить файл 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");

Мой последовательный монитор IDE ARDUINO возвращает:

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>

Не могли бы вы понять мою проблему?

Спасибо,

EDIT

Я продвинулся еще немного. Я создал php файл в каталоге posts . Теперь я получил HTTP-код 200, поэтому мой веб-сервер получил запрос POST.

Новая проблема: Я не могу понять, как отобразить содержимое POST в моем php-файле. У вас есть идея?

Спасибо,

1 Ответ

0 голосов
/ 26 ноября 2018

Я решил свою проблему, заменив:

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

от

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