Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Debug Error 400: Bad Request? (PHP)

Occassionally, I attempt to upload an mp3 to my server, it gets a "400: bad request" error. It sometimes works, sometime doesn't. I want to debug this!

I was wondering if there was a way to log the error via php, and the more verbose reason why it is a bad request? I've searched and cannot find a solution. thanks.

like image 966
c.m Avatar asked Aug 30 '26 10:08

c.m


1 Answers

First of all, you have to determine who is responsible for this error: the web server or your PHP code.

  • For the web server, it's simple: there will be an entry in the error log. Check it carefully. You can grep the log file for 400 error specifically, like grep 400 /path/to/error.log. There will be false positives, of course, but it will give you a starting point. After getting the actual error message from the log, you can Google/chatbot it for the explanation and possible remedies. Like, sometimes PHP devs make a mistake that causes uncontrolled growth for a cookie value, which will eventually cause this error.
  • For your PHP code, it's even simpler: there must be explicit code that responds with the error code, so you can search your codebase for "400" or look for functions like http_response_code(). After finding the place, you can add a more descriptive error message to make debugging easier the next time.

Another suggestion, which is less scientific but can be quicker, is checking the Dev console in your browser. Since 400 is an HTTP response, it's a problem with HTTP headers. And in the Network tab of Dev console, after selecting the problem request, you can observe the headers and try to spot some abnormality. Such as cookie size, for example.

like image 94
Your Common Sense Avatar answered Sep 02 '26 05:09

Your Common Sense



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!