Forums

Bad Request 400

Suddenly I am getting this error when I try to access my app. This has seemingly happened despite the fact that I have made absolutely no changes to any file... Thanks for any assistance.

A 400 error from a web app usually means that the view that you're accessing doesn't accept the HTTP method that was sent. For example, you may have a view that only accepts POST requests and you're sending it a GET request.

Yes, but it Did work when I initially set it up, and I have made no changes....

If you didn't reload your app when you made some changes a while ago, then you could have been running with code that worked up until we have to reboot a machine or restart your web app. Then the changes that you made, but didn't see in action would take effect.

"Bad Request (400)" errors are fairly common in Django apps (is that what you're using) when DEBUG is off and the ALLOWED_HOSTS setting is incorrect. Have a look at this SO question

Yes I am using Django. So are you saying i should change ALLOWED_HOSTS to '*'? Currently ALLOWED_HOST='/home/mjmacarty/source_directory/'. By the way, all was good until 6/12. Then I started getting 400. Looking at the log I sometimes get 499, with odd occasional 489.

I think ALLOWED_HOST should be something like mjmacarty.pythonanywhere.com, or whatever domain name you setup your webapp with.

Just to add to what Conrad said: ALLOWED_HOSTS should be a list of the domain names your site is hosted on. So if you have a web app at mjmacarty.pythonanywhere.com pointing to your codebase, it should be ["mjmacarty.pythonanywhere.com"].

(If you're doing something more complicated, and have multiple web apps on the "Web" tab all pointing to exactly the same codebase, then you need to list all of them there, eg. ["mjmacarty.pythonanywhere.com", "www.otherdomain.com", "www.andanother.com"])

However: ALLOWED_HOSTS is ignored if you have DEBUG set to True. So if you haven't changed anything at all recently -- not even switched off debug mode -- then perhaps that's not the cause. Do you have an example of a URL that triggers the error? And can we look at your code? (We can see it from out side, but we always ask before doing so.)

Thanks that did it. I misunderstood where the ALLOWED_HOSTS should point.

I made a help page about the django 400 bad request error, hope people find it useful!

Erreur 400 Bad Request

Did you check the help page linked above your post? Also, did you check your web app's error log?