ESP8266HTTPClient POST doesn't work and returns -1 as status code

I am trying to do a POST request to my Webserver that is running in a Kubernetes cluster.

If I do this request in postman, works fine:

When I try to do this request trought ESP8266 it does not work. The status code response is always -1.

I opened a Ngrok tunnel to my web server to find out what is going on and found that the ESP8266 request didn't even touch the application.

Here is my code:

main.cpp file

#include "http_requests.h"

const char* wifi_ssid = "ssid";
const char* wifi_password = "password";

void setup()
{
    Serial.begin(115200);
    delay(1000);

    WiFi.begin(wifi_ssid, wifi_password);
    delay(1000);
}

void loop()
{
    do_post();
    delay(3000);
}

http_requests.h file:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <json_utils.h>

String serverPath = "http://192.168.49.2:30055";

void do_post()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);

        client->setInsecure();

        HTTPClient http;

        // String path = serverPath + "/messages";
        String body = generate_message_json(WiFi.macAddress(), "TEST TESTE");
        // The method above is working fine, it generate the json as I need.

        http.begin(*client, "http://192.168.49.2:30055/messages");
        http.addHeader("Content-Type", "application/json; charset=UTF-8");

        Serial.println("body: ");
        Serial.print(body);

        int httpCode = http.POST(body);

        Serial.println("HTTP STATUS RESPONSE: ");
        Serial.println(httpCode); // always prints -1

        http.end();
    }
}

As I said, the function that creates JSON is working fine.

There is no error on the terminal.

What can I do to solve it?

you use secure client for a http request. it will not work.
simply use WiFiClient, not WiFiClientSecure

@Juraj same error using WiFiClient. Still having -1 as response.

is port 30055 open on firewall on 192.168.49.2?

@Juraj Yes, I can do a telnet to this IP and get connected.

I think the main problem is that HTTPClient does not even do a request. As I said, I opened a HTTP tunnel with Ngrok and nothing prints on Ngrok console.

I would like to see the version of your code with WiFiClient

Ok, the main.cpp does not change in nothing.

Here is the http_requests.h:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <json_utils.h>

String serverPath = "http://192.168.49.2:30055";

void do_post()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        WiFiClient client;

        HTTPClient http;

        String path = serverPath + "/messages";
        String body = generate_message_json(WiFi.macAddress(), "TEST TESTE");


        http.begin(client, "http://192.168.49.2:30055/messages");
        http.addHeader("Content-Type", "application/json; charset=UTF-8");

        Serial.println("body: ");
        Serial.print(body);

        int httpCode = http.POST(body);

        Serial.println("HTTP STATUS RESPONSE: ");
        Serial.println(httpCode);

        http.end();
    }
}

I would like to add some information, I did some packet sniffing with wireshark on my local K8S cluster and really, no requests made by ESP8266 arrives.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.