Go Down

Topic: Http error code (Read 42 times) previous topic - next topic

ebolisa

Sep 18, 2019, 10:02 pm Last Edit: Sep 18, 2019, 10:04 pm by ebolisa
Hi,

The function below sends a string to a database through a php code.

However, when run it, I get an error code but I'm not sure what I'm doing wrong.

Code: [Select]

httpRequestData: api_key=tPmAT5Ab3j7F9&value1=182.3.217.63
Error code: -1



If in place of the extIP I use a string like "123", it works fine.

Surely is the ip address which is the cause of the problem.

Any help is appreciated.
TIA


Code: [Select]
void db_send() {
  //Check WiFi connection status

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Your Domain name with URL path or IP address with path
    http.begin(serverName);

    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");

    // Prepare your HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + extIP + "&value2=" + 0 + "&value3=" + 0 + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);

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

    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }

    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
}

Power_Broker

You may need to encode your URL by replacing special characters (i.e. periods) with certain codes. In your case you would replace the periods with %2E.

URL Encoding Reference
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up