I am trying to get JSON data from my website using an HTTP GET request, but I'm getting a 400 bad request error. Can someone let me know why am I getting this error and how to fix that? I am tired of looking at online resources.
I am trying to fetch data from www.himalayavats.com/to_fetch/system_info.json
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 client
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 character to ESP8266
long int time = millis();
while( (time+timeout) > millis() ) {
while(ESP8266.available()) {
// The ESP 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:
AT+CWMODE=1
OK
AT+CWJAP="moto g","hvats555"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
AT+CIPMUX=1
OK
AT+CIPSTART=4,"TCP","himalayavats.com",80
4,CONNECT
OK
AT+CIPSEND=4,43
OK
>
Recv 43 bytes
SEND OK
+IPD,4,1370:HTTP/1.1 400 Bad Request
Date: 2017年12月19日 08:30:21 GMT
Server: Apache
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html
My JSON file on the website looks like this:
{"power" : "high"}
1 Answer 1
Take a look at the HTTP specification:
Request = Request-Line ; Section 5.1
*(( general-header ; Section 4.5
| request-header ; Section 5.3
| entity-header ) CRLF) ; Section 7.1
CRLF
[ message-body ] ; Section 4.3
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
You don't send the CRLF (carriage return, line feed) at the end of your request line and at the end of the header at the same time of the request. Why the delay?
You also miss the obligatory host header.
Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
-
getting the error without delay AT+CWMODE=1 OK AT+CWJAP="moto g","hvats555" 4,CLOSED WIFI DISCONNECT WIFI CONNECTED WIFI GOT IP OK AT+CIPMUX=1 AT+CIPSTART=4,"TCP","himalayavats.com",80 busy GET /to_fetch/system_info.json HTTP/1.1 ERRORHimalya vats– Himalya vats2017年12月19日 09:33:56 +00:00Commented Dec 19, 2017 at 9:33
delay(1000);
betweenESP8266.println(cmd);
andESP8266.println("");
? That's bound to break things...