Having issues with sending POST Request Data

Hello. Lately I was trying to use the ESP32 with the Arduino IDE to play around with WiFi Webservers and vice versa sending Webserver Requests with it as I plan to use this in a future project.

My issue is that 4/5 times the data sent by the Webclient Code via a POST Call is not received properly. I tried using Postman (application for sending test http Calls) and doing basically the same as with the Webclient Sketch and everything worked perfectly fine every time.

Note: My issue has nothing to do with JSON or the JSON Library I am using (that's a whole other issue :slight_smile: but my webserver is unable to receive the data my client should send.

Here is my Webserver Code:

// Load Wi-Fi library
#include <WiFi.h>
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "<changed for privacy>";
const char* password = "<changed for privacy>";

// Set web server port number to 5000
WiFiServer server(5000);

// Variable to store the HTTP request
String header;

DynamicJsonDocument json_data_http(1024);

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  Serial.begin(115200);
  Serial.println("Webserver Example");

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected() && currentTime - previousTime <= timeoutTime) {  // loop while the client's connected
      currentTime = millis();
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/json");
            client.println("Connection: close");
            client.println();
            
            String body_data = "";
            while(client.available()){
              char c = (char)client.read();
              body_data += c;
              Serial.print(c);
            }
            Serial.println();

            if (body_data.length() > 0) {
              Serial.print("Body Data: ");
              Serial.println(body_data);
              deserializeJson(json_data_http, body_data);
            }

            if (header.indexOf("POST /test") >= 0) {
              Serial.println("POST /test");
            }            
            client.println("done");
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

Note: the ArduinoJson Library is for later use in handling sent JSON Data. The "serializeJson" is basically for printing purposes.

The purpose of this sketch is to receive and print out the data that is being sent by the client.

And here is the Webclient Code:

// Load Wi-Fi library
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "<changed for privacy>";
const char* password = "<changed for privacy>";

const char* serverName = "http://192.168.1.110:5000/test";

void setup() {
  Serial.begin(115200);
  Serial.println("HTTP Request Example");

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println(WiFi.localIP());
}

void loop(){
  HTTPClient http;
  Serial.print("sending ");
  Serial.println(serverName);
  http.begin(serverName);
  http.addHeader("Content-Type", "application/json");

  int httpResponseCode = http.POST("{\"address\": \"192.168.1.110\"}");
  String payload = http.getString();
  
  Serial.println(httpResponseCode);
  Serial.println(payload);

  http.end();

  delay(2000);
}

The purpose of this sketch is to send a http POST Call every 2 seconds with some JSON data as payload.

are both controllers ESP32?

If yes, you should use the webserver for ESP32

Start with the example WebServer / Hello Server

You have access to your parameters via

server.args()
server.argName()
server.arg()