0

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"}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Dec 19, 2017 at 8:34
6
  • Why are you putting delay(1000); between ESP8266.println(cmd); and ESP8266.println("");? That's bound to break things... Commented Dec 19, 2017 at 8:55
  • @dda getting error on removing delay Commented Dec 19, 2017 at 10:29
  • What kind of error? Commented Dec 19, 2017 at 10:38
  • And anyway you should build your request so that the two CRLF are in the string, and send that. Commented Dec 19, 2017 at 10:39
  • 2
    the delays are a substitute for waiting on confirmation from the esp. better use a library that makes the AT communication for you. for example the WiFiEsp library github.com/bportaluri/WiFiEsp Commented Dec 19, 2017 at 10:50

1 Answer 1

2

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
answered Dec 19, 2017 at 9:06
1
  • 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 ERROR Commented Dec 19, 2017 at 9:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.