ESP8266 will talk to REST API's except for the one I've made

Hi this is my first post so if I do anything wrong I'm sorry!
I currently have a Express REST API running on my mac, my mac has a static IP address of 192.168.1.9 on the LAN and the server is running on port 3000

I created a GET route to get data from a database on my machine, if I use postman on it from the server machine or another machine then each machine receives a response hence why I don't think the server is the problem

When I try to do the same GET request on my DFROBOT Fire-beetle ESP8266 board then it comes up with a WiFi 3 status code (connected) but a http response code of -1 (no response from server) which would indicate that the board isn't working, however when I changed the address to "http://numbersapi.com/random/math" then it worked absolutely fine. I have no idea what the problem with the code is and I swear it was working just fine yesterday (although intermittently it would disconnect"

Please help

Heres the Arduino code:

// Dependencies
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>


// Variables
const String ssid = "[INSERT NETWORK SSID]";
const String pass = "[INSERT NETWORK PASSWORD]";
const String serverIP = "192.168.1.9:3000"; //static ip of my mac with api running

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

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

  delay(1000); Serial.print("\n");

  WiFi.begin(ssid, pass);
  Serial.print("Connecting to " + ssid);
  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.print(".");
  }
  Serial.print("\nConnection Established with IP: "); Serial.println(WiFi.localIP());
}


void loop() {
  // Send an HTTP GET request depending on timerDelay
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;

      String serverPath = serverIP + "/api/machines";
      
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
        Serial.println(WiFi.status());
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

OUTPUT:
Connecting to SSID.........
Connection Established with IP: 192.168.1.107
Error code: -1
3

https://stackoverflow.com/questions/72303614/esp8266-will-talk-to-rest-api-s-except-for-the-one-i-ve-made

Thats my stackoverflow post. I'm trying to get help from anywhere :sweat_smile: @Juraj

Is your API serving http or https?

http

I suggest that you check your web server logs for clues. After that, I'd get a copy of Wireshark and look at packets.

Error code: -1 says it didn't connect to the server. so there will be nothing in server logs

Copied the example code (Thanks Rui Santos) and it seemed to start working. My betting is there is some small error in there but at least it's working now :sweat_smile:

Credit: ESP8266 NodeMCU HTTP GET and HTTP POST with Arduino IDE | Random Nerd Tutorials

Thanks for trying to help @Juraj @wildbill

@Juraj Answered my question on StackOverflow!
(https://stackoverflow.com/questions/72303614/esp8266-will-talk-to-rest-api-s-except-for-the-one-i-ve-made)
I missed the "http://" before the IP Address!

Thanks @Juraj

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