Catching connection failure for http.GET()

Hi, can someone please advise on the correct method of catching connection error or failure in http.GET() requests? Just wondering if there's a builtin that I can use for a while statement to ensure any failure to connect will result in retry.

Current code. Note the credentials and tagMatch are global and cleared at the beginning of the function.

Thanks in advance!

void getRfidMatch() {

  // Empty Credentials
  loadID[0] = '\0' ;
  hiveNum[0] = '\0' ;
  boxNum[0] = '\0' ;
  rfidMatchChar[0] = '\0' ;
  tagMatch = false ;

  char GET_URL[150] = "" ;
  append("http://", GET_URL) ;
  char IPChar[16] ;
  serverIp.toString().toCharArray(IPChar, 16) ;
  append(IPChar, GET_URL) ;
  append("/rfid2hiveid_0.3_secure.php?", GET_URL) ;

  // Format GET request
  append2("api_key=", apiKeyValue, GET_URL) ;
  append2("&pass=", sqlPass, GET_URL) ;
  append2("&rfid=", sqlUID, GET_URL) ;

  // Send formatted GET request
  HTTPClient http ;  // Declare object of class HTTPClient
  http.begin(GET_URL);
  int httpResponseCode = http.GET()
  String payload = http.getString();    //Get the response payload
  if (payload == "0 results") { // if there's no match, report
    tagMatch = false;
    return;
  }
  
  // If sql record returned form server
  tagMatch = true ;
  // assign variable values from payload
  int payload_len = payload.length() + 1;
  payload.toCharArray(rfidMatchChar, payload_len); // place returned ":" delimeted data into char arrays
  http.end();
}
  
  // If sql reqcord returned form server
  tagMatch = true ;
  // assign variable values from payload
  int payload_len = payload.length() + 1;
  payload.toCharArray(rfidMatchChar, payload_len); // place returned ":" delimeted data into char arrays
  http.end();
}

the GET function returns an error code as an int, stored into variable httpResponseCode

the error code tells you whether or not the request was okay,
the list is here

in your case you have to check for 200 before invoking
http.getString()

like this

int httpResponseCode = http.GET();
Serial.print("error code: ");
Serial.println(httpResponseCode);
if ( httpResponseCode  == 200 ) {
  String payload = http.getString();
 // other lines go here
} 

not sure If I have answered what you asked

Thanks for your reply.

Yes your answer makes perfect sense.

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