Search code examples
jsonhttppostarduinoesp32

ESP32 gives error on HTTP Post to Flask server


My goal is to post data to a Flask server. For this I have the following code running on a computer(Jupyter):

from flask import Flask
    
    from flask import request
    
    app = Flask(__name__)
    
    @app.route('/postjson', methods = ['POST'])
    
    def postJsonHandler():
    
        print (request.is_json)
    
        content = request.get_json()
    
        print (content)
    
        return 'JSON posted'
    
      
    
    app.run(host='0.0.0.0', port= 8090)

On the esp I have the following function responsible for posting, Right now it is just for testing , I will further the functionality later on.

//Posts data to server
void post_to_server(String url)
{
  HTTPClient http;

  // Prepare JSON document
  JsonObject root = doc.to<JsonObject>();
  JsonArray pressure = root.createNestedArray("pressure");
  JsonArray time = root.createNestedArray("time");

  pressure.add("Pressure");
  time.add("Time");

  // Serialize JSON document
  String json;
  serializeJson(root, json);

  // Send request
  http.begin(url);
  http.addHeader("Content-Type", "application/json");

  int httpResponseCode = http.POST(json); //Send the actual POST request

  // Read response
  Serial.print(http.getString());

  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);

    // Disconnect
    http.end();
  }
}

So here is the odd thing, when I call the function on a test server like this:

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

It works and I get the following response on the Serial Monitor as expected:

 {
  "pressure": [
    "Pressure" 
  ],
  "time": [
    "Time"
  ],
  "id": 101

But when I try to post to the Server running on my PC like this:

 post_to_server("http://127.0.0.1:8090/postjson");

I get the following error:

0
[E][WiFiClient.cpp:258] connect(): socket error on fd 54, errno: 104, "Connection reset by peer"
Error on sending POST: -1

I cant really make sense of this so I came here. I would appriciate any help. I also get the following when I test on Postman:

enter image description here


Solution

  • post_to_server("http://127.0.0.1:8090/postjson");
    

    This will never work on your ESP32.

    127.0.0.1 is the "loopback address" - the same as the name localhost. It's shorthand meaning "this computer".

    When you use this with a program you run on your Windows machine, the program will attempt to connect to the Windows machine.

    When you use this with your ESP32, it means connection to the ESP32.

    You need to use the IP address associated with your Windows machine's network connection, whether ethernet or WiFi. 127.0.0.1 will not work.