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?