Elmi
Elmi

Reputation: 6193

ESP32: HttpClient.get() fails with -1

I'm programming my ESP32 with the ArduinoIDE and have a problem with HTTP GET. What I'm doing:

This is the code I'm using for the HTTP GET call:

static WiFiClient wifi;

HttpClient wlanHttp=HttpClient(wifi,"my.server.tld");
wlanHttp.get("/setpos.php?id=DEADBEEF"); // -> this fails with error code -1
wlanHttp.responseStatusCode(); // follow-up error -1
wlanHttp.stop();

Any idea what goes wrong here?

Upvotes: 0

Views: 5592

Answers (2)

Codebreaker007
Codebreaker007

Reputation: 2989

On ESp32 (if using the HTTPClient.h) the code should look like that:

static WiFiClient wifi;
HttpClient wlanHttp;

wlanHttp.begin("http://my.server.tld/setpos.php?id=DEADBEEF"); //Specify the URL
int httpCode = wlanHttp.GET();                                 //Make the request

if (httpCode > 0) { //Check for the returning code
  if (httpCode == HTTP_CODE_OK) { 
    // get payload with http.getString();
    Serial.println(httpCode);
    // Serial.println(payload);
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n", wlanHttp.errorToString(httpCode).c_str());
  }
} else {
  Serial.println("Error on HTTP request");
}

wlanHttp.end(); //Free the resources

Upvotes: 0

Jessica
Jessica

Reputation: 41

The confusing part here is the ESP32 has a built in http client called HTTPClient. The one for Arduino is called HttpClient and I'd like to find the guy who decided on that name and see if he's okay. HTTPClient has a routine called getString() that is a lovely way to gather info from a json api call, but HttpClient won't compile with that because it has no clue what that is.

Upvotes: 4

Related Questions