Page 1 of 1

ESP32 using HTTPClient library to call API

Posted: Tue Oct 23, 2018 5:35 pm
by gandrade11
Hi, I'm having some problem, that maybe is really easy, I hope you can help me.

I'm using ESP32 and the library HTTPClient to call an API, like that.

Code: Select all

HTTPClient http;   
         
http.begin("http://130.101.93.116/resource/comanda");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");             //Specify content-type header
int httpResponseCode = http.GET();
if(httpResponseCode>0){
    String response = http.getString();                       //Get the response to the request
    Serial.print("httpResponseCode: ");   //Print return code
    Serial.println(httpResponseCode);
    Serial.println(response);           //Print request answer
     }else{
    Serial.print("Error on sending GET: ");
    Serial.println(http.errorToString(httpResponseCode));
}
http.end();
When I run the get through the browser I get my normal database data, but by esp32 I have that answer below. I would like to know how to receive the normal data because as it seems to me, the function that is in my code is not calling API.

Code: Select all

httpResponseCode: 200
<html>
<head>
<meta http-equiv='refresh' content='1; url=http://130.101.93.116/resource/comanda&arubalp=298629fd-4805-4e2c-bd14-9b616ceb85'>
</head>
</html>
Just to clarify, I received this data when I use the browser.

Re: ESP32 using HTTPClient library to call API

Posted: Wed Oct 24, 2018 2:17 am
by ESP_Sprite
The webserver uses a html-based http header to redirect you to another URL (which is pretty dirty; it's cleaner to use a http 302 redirect, especially for m2m APIs). Your webbrowser interprets the HTML (as it needs to render it) and automatically executes the redirect. Your ESP32 does not have HTML interpretation capabilities and just spits out the page, including the redirect code.