Search code examples
arduinohttprequestesp8266arduino-c++

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: enter image description here

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

        //PS: I Tryied to use WiFiClient and got same error.

        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?

edit:

Here are some debug logs:

SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
fpm close 3 
mode : softAP(ae:0b:fb:da:97:f7)
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
bcn 0
del if1
usl
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
Server listening
Connecting to Wifi
mode : sta(ac:0b:fb:da:97:f7) + softAP(ae:0b:fb:da:97:f7)
add if0
bcn 0
del if1
mode : sta(ac:0b:fb:da:97:f7)
..scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 

connected with MY HOME WIFI, channel 4
ip:192.168.1.50,mask:255.255.255.0,gw:192.168.1.1
ip:192.168.1.50,mask:255.255.255.0,gw:192.168.1.1
.Connected
[HTTP-Client][begin] url: http://192.168.49.2:31230/messages
[HTTP-Client][begin] host: 192.168.49.2 port: 31230 url: /messages
body: 
{"macAddress":"AC:0B:FB:DA:97:F7","operation":"TEST TESTE"}

[HTTP-Client][sendRequest] type: 'POST' redirCount: 0
[HTTP-Client] failed connect to 192.168.49.2:31230
[HTTP-Client][returnError] error(-1): connection failed
HTTP STATUS RESPONSE: 
-1

Solution

  • The problem was because I was using Minikube.

    Minikube has a lot of abstractions, including it own network. It means that my application was unreachable.

    So I discarded Minikube and started to use microk8s.

    Now it is working.