Http requests to Localhost from esp8266

I am working with esp8266, and I want to access some data from my php laravel apis hosted on my laptop, so far I am unable to do so.
I have set up a mobile hotspot on my laptop and connected my phone to the laptop's hotspot.
My laptop IPv4 is : http://192.168.100.40/
and postman gives appropriate responses.
Also I check on my mobile device by connecting it to laptop's hotspot and I get appropriate responses.

here is my esp8266 code:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
// #include <HttpClient.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
DNSServer dnsServer;
WiFiManager wifiManager;

void setup() {
  Serial.begin(9600);
  // WiFi.disconnect(true);
  // ESP.eraseConfig();
  // Connect to WiFi
  WiFiManager wifiManager;
  wifiManager.setTitle("esp");
  wifiManager.setDarkMode(true);
  wifiManager.autoConnect("esp");
  // Print MAC address
  Serial.print("MAC address: ");
  Serial.println(WiFi.macAddress());
  // Configure mDNS
  if (!MDNS.begin("esp")) {
    Serial.println("Error setting up MDNS responder!");
    while(1) { 
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");
  
  // Start DNS server
  dnsServer.start(53, "*", WiFi.softAPIP());


  server.begin();
  Serial.println("HTTP server started");
  Serial.print("entering handle root");
  // Retrieve users data
 WiFiClient client;
  HTTPClient http;

  String url = "http://192.168.100.40:8000/api/users";

  if (http.begin(client, url)) {
    int httpCode = http.GET();

    if (httpCode > 0) {
      Serial.print(httpCode);
      if (httpCode == HTTP_CODE_OK) {

        String payload = http.getString();
        Serial.println("HTTP GET request successful");
        Serial.println("Response: " + payload);
      }
    } else {
      Serial.println("HTTP GET request failed");
    }

    http.end();
  } else {
    Serial.println("Unable to connect to server");
  }
  
}

void loop() {
  server.handleClient();
  MDNS.update();
}

Serial monitor response: HTTP GET request failed

please print

httpCode

and tell us what you get.

PS.: section

Using Arduino / Networking, Protocols, and Devices

is more suitable for your topic. You can move your topic when you press edit the header

it doesn't get to httpcode, it fails the first check if (http.begin(client, url))
and thanks for the suggestion, I will do that

it does, otherwise it would not print what you have written.
If begin would fail, it should write

Unable to connect to server

you are right, I overlooked it

thanks for your help, but I just figured out the problem
the problem was the firewall and not the code itself, I configured firewall to enable inbound requests for ports 80 and 3000, and response is received from the localhost.

.
Good to hear.
You can mark your thread as solved.
You can say thank you to helpers with the heart.

1 Like

alright, I will do that

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