REST API POST request ESP32 local network REFUSED

hello so iwant to use POST request from my esp32 on local network, but keep having connection refused, knowing that post request to public network is successfully established

here's my code


`#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>


// WiFi credentials
const char* ssid = "Marina cafe";
const char* password = "marinaw1623";

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);

  // Wait for the connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Print the assigned IP address to the Serial Monitor
  Serial.print("ESP32 IP Address: ");
  Serial.println(WiFi.localIP());

  // Now, let's send a POST request to the Flask server using the full URL
  HTTPClient http;
  
  // Full URL for the server (adjust the URL if it's HTTPS)
  String serverUrl = "http://192.168.1.134:5000/send_message/";
  
  // Connect to the server
  http.begin(serverUrl);  // Here, we use the full URL directly
  http.addHeader("Content-Type", "application/json");  // Add header for JSON content

  // For HTTPS connections, you can use the setInsecure() method to skip SSL certificate validation
  // If using HTTPS, uncomment the next line:
  // http.setInsecure();  // Insecure option to trust the server (bypasses SSL verification)

  // Sample JSON payload to send
  String payload = "{\"message\": \"Hello from ESP32!\"}";

  // Send POST request
  int httpResponseCode = http.POST(payload);

  // Check for errors or success
  if (httpResponseCode > 0) {
    // If the POST request is successful
    Serial.println("POST request successful");
    Serial.println("HTTP Response Code: " + String(httpResponseCode));
    String response = http.getString();  // Get the server's response
    Serial.println("Response: " + response);
  } else {
    // If there was an error sending the request
    Serial.println("Error on HTTP request");
    Serial.println("HTTP Response Code: " + String(httpResponseCode));
    Serial.println("Error message: " + http.errorToString(httpResponseCode));  // Get more details about the error
  }

  // End the HTTP connection
  http.end();
}

void loop() {
  // Nothing to do in loop for now
}

and this is prints from serial monitor

en:3524
entry 0x400805b8

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4916
load:0x40078000,len:16436
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3524
entry 0x400805b8
Connecting to WiFi...
Connecting to WiFi...
Connecting to WiFi...
Connecting to WiFi...
Connected to WiFi
ESP32 IP Address: 192.168.1.43
Error on HTTP request
HTTP Response Code: -1
Error message: connection refused

Plkease edit your post and add code tags, then do the same for the error msgs.

1 Like

Welcome to the forum.

Please read How To Get The Best Out of This Forum. Then, format your code using code tags, as the article explains.

You probably want to start slowly and add complexity as you progress.

I suggest trying a simple HTTP post example and then adding to it once things are working.

2 Likes

Done

:white_check_mark:done

Right, I replaced the url with an echo service

  String serverUrl = "http://httpbin.org/anything";

and it worked. So the problem is probably with the local server. The obvious check first: did you try it?

$ curl -i http://192.168.1.134:5000/send_message/

What is the reported localIP for the ESP32? Are you sure they're on the same local network?

You can also try, under the IDE Tools menu, about half-way down, setting the Core Debug Level to Debug instead of None. Then re-Upload. You'll get more messages in the Serial Monitor about what the HTTPClient (and everything else with ESP32) is doing.

1 Like

yesss sure they are both on same network (laptop (server) and the esp32)), still even changing the server port, trying different other servers, messing with network masks etc, still getting connection refused (only this behavior happens when POST a PRIVATE ip on my lan, if i switch to POST to a public ip, all runs smoothly

Are you using an Arduino Nano ESP32 or another ESP32?

What did the Debug info print before the Error on HTTP request?

Does it work with GET instead of POST? (I'm guessing no.)

Did you try another computer or even a phone on the same LAN? With a computer, in addition to curl, you could try telnet or nc.

1 Like

im using lilygo q336 tcell sim800h, it has an esp32, curling the url from my laptop responds correctly, through the esp it says connection refused no matter what port im using

yesss same with GET, connection refused, and yes i tryed and it succeed, else on the esp32 it doesnt allow, i've been switching the server from flask nodejs or even linux server,and the esp32 always reject connecting to them, weird behavior

so turning wifi from public to private solved the problem ! public wifi disable local communication between components

1 Like

Spent a day working on a very similar problem.
But, the problem all came down to my URI which had 'HTTP://something.org...' at
the beginning.
It only worked with 'http://something.org..