Flask - Resolve "400 Bad Request: The browser (or proxy) sent a request that this server could not understand"
by
Jeremy Canfield |
Updated: November 06 2023
| Flask articles
Let's say something like this is being returned.
400 Bad Request: The browser (or proxy) sent a request that this server could not understand
I once got this when attempting to submit a POST request to my Flask app. For example, my Flask App had a route like this that expected JSON.
@views.route('/api/v1/example', methods=['GET', 'POST'])
def apiv1example():
try:
json = request.get_json()
except Exception as exception:
print(exception)
And I had a Python script making a POST Request to my Flask app.
#!/usr/bin/python
import requests
url = "http://www.example.com/api/v1/example"
payload = { "hello": "world" }
headers = { "Content-Type": "application/json" }
requests.post(url, data=payload, headers=headers)
This issue was that request.post had "data" instead of "json".
requests.post(url, data=payload, headers=headers)
I simply had to just change "data" to "json".
requests.post(url, json=payload, headers=headers)
Did you find this article helpful?
If so, consider buying me a coffee over at