I am making trying to get JSON data from my website using an HTTP getGET request, but I'm getting a 400 bad request error, Can. Can someone helplet me to know why am I getting 400this error and how to fix that,? I am tired of looking at online resources I.
I am trying to fetch data from- www.himalayavats.com/to_fetch/system_info.json I
I am using this code:-
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);
unsigned long lastTimeMillis = 0;
void setup() {
sendData("AT+RST\r\n",1000, true);
Serial.begin(9600);
ESP8266.begin(9600);
delay(2000);
connectToWifi();
}
void connectToWifi() {
String ssid = "moto g";
String pass = "hvats555";
sendData("AT+CWMODE=1\r\n", 3000, true); // Configure as clintclient
sendData("AT+CWJAP=\"moto g\",\"hvats555\"\r\n", 5000, true); // Connects to wifi
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
ESP8266.print(command); // send the read charactecharacter to esp8266ESP8266
long int time = millis();
while( (time+timeout) > millis() ) {
while(ESP8266.available()) {
// The espESP has data so display its output to serial window
char c = ESP8266.read(); // read the next character
response+=c;
}
}
if(debug) {
Serial.print(response);
}
return response;
}
void printResponse() {
while (ESP8266.available()) {
Serial.println(ESP8266.readStringUntil('\n'));
}
}
void loop() {
if (millis() - lastTimeMillis > 30000) {
lastTimeMillis = millis();
ESP8266.println("AT+CIPMUX=1");
delay(1000);
printResponse();
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"himalayavats.com\", 80");
delay(1000);
printResponse();
String cmd = "GET /to_fetch/system_info.json HTTP/1.1";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
ESP8266.println(cmd);
delay(1000);
ESP8266.println("");
}
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
}
I get this response every time-:
My jsonJSON file on the website looks like this: