Skip to main content
deleted 139 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

esp8266 ESP8266 returning 400 bad request

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:

esp8266 returning 400 bad request

I am making trying to get JSON data from my website using HTTP get request but getting 400 bad request error, Can someone help me to know why am I getting 400 error and how to fix that, I am tired looking online resources I am trying to fetch data from- www.himalayavats.com/to_fetch/system_info.json I am using 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 clint
      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 characte 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-

My json file on website looks like this:

ESP8266 returning 400 bad request

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:

My JSON file on the website looks like this:

Source Link

esp8266 returning 400 bad request

I am making trying to get JSON data from my website using HTTP get request but getting 400 bad request error, Can someone help me to know why am I getting 400 error and how to fix that, I am tired looking online resources I am trying to fetch data from- www.himalayavats.com/to_fetch/system_info.json I am using 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 clint
      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 characte 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: Tue, 19 Dec 2017 08:30:21 GMT
Server: Apache
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html

My json file on website looks like this:

{"power" : "high"}