ESP32 using HTTPClient library to call API

gandrade11
Posts: 1
Joined: Fri Oct 12, 2018 12:31 pm

ESP32 using HTTPClient library to call API

Postby gandrade11 » Tue Oct 23, 2018 5:35 pm

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.
Attachments
Capture.PNG
Capture.PNG (26.26 KiB) Viewed 4226 times

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 using HTTPClient library to call API

Postby ESP_Sprite » Wed Oct 24, 2018 2:17 am

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.

Who is online

Users browsing this forum: No registered users and 77 guests