"Send payload error" in HTTP POST - ESP8266 in Arduino IDE

I’m trying to make a POST HTTP request via ESP8266 using Arduino IDE and this library, but for any reason I’m getting this error:

[HTTP] POST... failed, error: send payload failed

Wifi connect’s correctly and header seems to be processed correctly in the library and the same request is working in POSTMAN.

By researching a little in the library, especifically in this file ESP8266HTTPClient.cpp, this section of code is where the error is trigged, and it’s because the call _tcp->write(&payload[0], size) returns 0 while size is 128 (the correct lenght of the payload I’m sending).

    // send Payload if needed
    if(payload && size > 0) {
        if(_tcp->write(&payload[0], size) != size) {
            return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
        }
    }

In the variable payload I’m sending the following (I also checked this is correct in POSTMAN):

{\"mode\":\"sync\",\"messageType\":\"4a0f64bf6fc780e39a51\",\"messages\":[{\"Humidity\":90,\"Temperature\":50,\"Brightness\":60,\"timestamp\":1}]}

Here is my code in Arduino IDE:

    #include <Arduino.h>
    #define DEBUG_ESP_HTTP_CLIENT
    #include <ESP8266WiFi.h>
    #include <ESP8266WiFiMulti.h>
    
    //#include <ESP8266HTTPClient.h>
    
    #define USE_SERIAL Serial
    
    #include "ESP8266HTTPClient2.h"
    
    ESP8266WiFiMulti WiFiMulti;
    
    void setup() {
        USE_SERIAL.begin(115200);
    
        USE_SERIAL.println();
        
        for(uint8_t t = 4; t > 0; t--) {
            USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
            USE_SERIAL.flush();
            delay(1000);
        }
    
        WiFiMulti.addAP("xxx", "xxx");
    }
    
    void loop() {
     // wait for WiFi connection
        if((WiFiMulti.run() == WL_CONNECTED)) {
    
            HTTPClient http;
    
            USE_SERIAL.print("\n---------------------\n[HTTP] begin...\n");
            // configure traged server and url
    
            http.setReuse(true);
            http.begin("iotmmsp1941648609trial.hanatrial.ondemand.com",
                        443,
                        "/com.sap.iotservices.mms/v1/api/http/data/ab5b0ae0-9e2c-47e2-ac93-0deea935a588");
                        
            http.setAuthorization("Bearer 77eb2493769bb566f5bc346f6d598e7");
            //http.addHeader("Authorization", "Bearer 77eb2493769bb566f5bc346f6d598e7");
            http.addHeader("Content-Type", "application/json;charset=utf-8");
            http.addHeader("cache-control", "no-cache");
    
            USE_SERIAL.print("[HTTP] POST...\n");
            // start connection and send HTTP header and body
            int httpCode = http.POST("{\"mode\":\"sync\",\"messageType\":\"4a0f64bf6fc780e39a51\",\"messages\":[{\"Humidity\":90,\"Temperature\":50,\"Brightness\":60,\"timestamp\":1}]}");
    
            // httpCode will be negative on error
            if(httpCode > 0) {
                // HTTP header has been send and Server response header has been handled
                USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);
    
                // file found at server
                if(httpCode == HTTP_CODE_OK) {
                    String payload = http.getString();
                    USE_SERIAL.println(payload);
                }
            } else {
                USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
            }
    
            http.end();
        }
    
        delay(3000);
    }

Question:

Why is that part of the code in ESP8266HTTPClient failing? Why it’s not sending the payload correctly?

Hello, I have the same error. I would like to know if it's possible to send a POST request with Content-Type: application/json.

Why do either of you want to POST? GET requests are much simpler to make. POST requests are used when you don't want anyone intercepting the packet to understand its contents. I can't see anything in the temperature, humidity, or brightness that needs to be kept secret.

alonsocarvajal:
Hello, I have the same error. I would like to know if it's possible to send a POST request with Content-Type: application/json.

One error in the old post is it connects to port 443 (SSL) but not open an SSL connection, as far as I can tell. If you are having problems, it could be very different from the original post so start a new topic and post your code.