I have a simple flask app
@app.route("/endpoint/", methods=['POST'])
def mypostmethod():
if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
post_data = request.json
req_data = request.get_json()
content = req_data['content']
return content
and I curl as follows:
curl -X POST "localhost:8080/endpoint/" -H "Content-Type: application/json" -d '{'content': 'Blah'}'
I get the following error:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</p>
It is because, json body in cURL is using single quotes, when I use double quotes it works.
I was wondering, how can I add some code in my flask app to check this error and return a custom response code and some response message? Where do I add the try block? Can you please use my code as example.