SOLVED : Help I got 400 bad request error, send data from esp32 to database mySQL

hi everyone, i got error code 400 while sending data string barcode from esp32 to mySQL database.


but when the sensor reading is not correct, the data can be sent to the database

my code

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <ESP32Servo.h> 

Servo servo1;
Servo servo2;
Servo servo3;

HardwareSerial MySerial(2);

const char* ssid = "RidwanSoleh";
const char* password = "ridwananaksoleh";

String kodebarcode ="";
String typea = "A";
String typeb = "B";
String typec = "C";
String kategori = typea ;
String perusahaan = "PT_Cinta_Sejati";
int jumlah = 1;


//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.0.196:80/sobsite/update_fcn.php";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 
MySerial.begin(115200, SERIAL_8N1, 16, 17);
  servo1.attach(4);
  servo2.attach(2);
  servo3.attach(5);
  
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
//  Pembacaan GPS


  
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "?kodebarcode=" + kodebarcode + "&kategori=" + kategori + "&perusahaan=" + perusahaan + "&jumlah=" + jumlah;

      Serial.println(serverPath);
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
while (MySerial.available() > 0) {
    kodebarcode = MySerial.readString();
    Serial.println(kodebarcode);
    Serial.println(kodebarcode.substring(0, 2));
    if (kodebarcode.substring(0, 2) == "A0") {
      Serial.println(typea);
    }
    else if (kodebarcode.substring(0, 2) == "B0"){
      Serial.println(typeb);}
    else if (kodebarcode.substring(0, 2) == "C0"){
      Serial.println(typec);}  
    else{Serial.println("Tidak Terdeteksi");}
  }
  servo1.write(0);
  if(kodebarcode.substring(0, 2) == "A0"){
    servo1.write(90);}  
    else{servo1.write(0);}
  servo2.write(0);
  if(kodebarcode.substring(0, 2) == "B0"){
    servo2.write (90);}
    else{servo2.write(0);}
  servo3.write(0);
  if(kodebarcode.substring(0, 2) == "C0"){
    servo3.write(90);}
    else{servo3.write(0);}
  
}

I still haven't found the solution

the barcode code has been read, but like this

</body></html>

http://192.168.0.67:80/sobsite/update_fcn.php?kodebarcode=A0716272
&kategori=A&perusahaan=PT.CintaSejati&jumlah=1
HTTP Response code: 400
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.0.3 Server at localhost Port 80</address>
</body></html>

I'm still facing the same error, when I enter the link from the serial monitor to the web browser, the data goes to the database

What do you see in your server logs?

How to see the logs ?

Also, check what you're actually hitting in the browser. That 302 may indicate that your site requires https and the browser is redirecting, but your ESP isn't equipped to do so.

ahh i see, but now i have a problem with bad request 400, when i fill in the data manually it can enter

Solved, I use POST methode

can you send me the example with POST method? i have the same error

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