+IPD 200 (Success) then "400 Bad Request"

Greeting all, :smiley:
I am working on a simple IoT device (ESP 8266)
Tried to send request on local Server (XAMPP). After following serveral if not all similar topics I was able to send data successfully from dht11 to xampp database.

The problem is after successful entry of Data into database, the HTTP response is followed by a 400 Bad request. It's quite disturbing since data has been sent.

I would like to know where I am going wrong or an explanation as to why i get two responses from one request.

Thanks in advance! :smiley:

Below is my code

float humidity;
float temperature;
String ssid = "Wifi";
String psk = "psk;
String host = " 192.168.45.110";
String getData;
String myHost = "Host:"+host;
void setup() {
  Serial.begin(9600);
  Serial.println("Serial Connected!");
  dht.begin();
  mySerial.begin(9600);
  while(!mySerial){}
  delay(1000);
  sendCmd("AT", "OK");
  sendCmd("AT+CWMODE_CUR=1", "OK");
  sendCmd("AT+CWJAP_CUR=\""+ssid+"\",\""+psk+"\"", "OK");
  delay(500);
}

void loop() { 
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();

  getData = "GET /esp/index.php?temp="+String(temperature)+"&humid="+String(humidity)+"\r\nHTTP/1.1\r\n"+myHost+"\r\nConnection:close\r\n\r\n";
  sendCmd("AT+CIPSTART=\"TCP\",\""+host+"\",80", "OK");
  sendCmd("AT+CIPSEND="+String(getData.length()), ">");  
  mySerial.print(getData);
  checkSerial();
}

void checkSerial(){
  if (mySerial.available()) {
    Serial.println(mySerial.readString());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

void sendCmd(String cmd, char reply[]){
  Serial.println(cmd);  
  int maxTime = 10;
  int counter = 0;
  while(counter<maxTime){
    mySerial.println(cmd);
    if(mySerial.find(reply)){
      Serial.println("Success");
      break;
    }else{
      while(mySerial.available()){
        Serial.write(mySerial.read());
      }
    }
    counter++;
    delay(500);
  }
  if(counter == maxTime){
    Serial.println("Connection Timed out");
  }
}

Recv 95 bytes

SEND OK

+IPD,382:DB Created
TBL Created
Data inserted
//output from .php file if data is inserted into db.

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.


Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.14 Server at localhost Port 80 CLOSED

400 Bad Request is a response from the server related to HTTP protocol.
SEND OK and +IPD is related to the TCP transport which was successful.

"Host:" is missing in your HTTP request string

Thanks for the reply,

But the host is part of the getData String

String host = " 192.168.45.110";
String getData;
String myHost = "Host:"+host;

getData = "GET /esp/index.php?temp="+String(temperature)+"&humid="+String(humidity)+"\r\nHTTP/1.1\r\n"+myHost+"\r\nConnection:close\r\n\r\n";

remove the first "\r\n" from "\r\nHTTP/1.1\r\n"

Thanks but I'm still getting the same response

+IPD,601:HTTP/1.1 200 OK
Date: Wed, 04 May 2022 09:25:02 GMT :confused:
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.14
X-Powered-By: PHP/7.3.14
Content-Length: 382
Connection: close
Content-Type: text/html; charset=UTF-8

DB Created
TBL Created
Data inserted

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.


Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.14 Server at localhost Port 80 CLOSED

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.