Parsing input failed!
HTTP Response code: 302
this mesage hapen when i try to get from url and this url serve another path url the problem is i cannot or i have not the expirien to found the new url if anybody can help
the code is bellow
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = “SSid”;
const char* password = “password”;
//Your Domain name with URL path or IP address with path
const char* serverName = “http://stream.radiojar.com/rbx9n33vdg0uv”; // the url i try
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
String sensorReadings;
float sensorReadingsArr[3];
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
Serial.println(“Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.”);
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() – lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
sensorReadings = httpGETRequest(serverName);
Serial.println(sensorReadings);
JSONVar myObject = JSON.parse(sensorReadings);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == “undefined”) {
Serial.println(“Parsing input failed!”);
return;
}
Serial.print(“JSON object = “);
Serial.println(myObject);
// myObject.keys() can be used to get an array of all the keys in the object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
Serial.print(keys[i]);
Serial.print(” = “);
Serial.println(value);
sensorReadingsArr[i] = double(value);
}
Serial.print(“1 = “);
Serial.println(sensorReadingsArr[0]);
Serial.print(“2 = “);
Serial.println(sensorReadingsArr[1]);
Serial.print(“3 = “);
Serial.println(sensorReadingsArr[2]);
}
else {
Serial.println(“WiFi Disconnected”);
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = “{}”;
if (httpResponseCode>0) {
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print(“Error code: “);
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
What exactly are you trying to do when accessing this URL with your ESP32 board?
Regards,
Rui
I believe you have a couple options here. Note that the following was gleaned from the HTTPClient.h file.
First (And probably easiest) is to force all redirects. After http.begin add a statement http.setFollowRedirects(2); I believe redirects are set to off normally.
Second, you could check the response headers using http.collectHeaders for a ‘Location’ header and do the redirect yourself. There is a tutorial that uses the 8266 but I believe the HTTPClient code is basically the same. You can see how the 302 response actually works.
Mr Steve Mercer thank you for quik answer AFTER your instraction this i try to do it works i bild an internet radio (not finish yet) and the problem is some station have redirection in the end of septeber i finish so i post here the code for everybody .
Rui Santos thank you for quik answer