Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask request.get_json() raise BadRequest

Tags:

python

json

flask

I have a Flask API with this endpoint :

@app.route('/classify/', methods=['POST'])
def classify():
    data = request.get_json()

When I POST a request with python, eveything is fine.

But when I use Postman, I get :

<class 'werkzeug.exceptions.BadRequest'> : 400: Bad Request

I send the same Json with both (I copy/paste it to be sure). I am fairly confident the issue is caused by some "\t" in my json, which are escaped by python but not by Postman.

Is there a way to retrieve the raw json, and process it (escape what needs to be escaped) in the app ? Or another way to get the json ?

EDIT : This is a different question from the one you suggested as duplicate, because yours suggest to use get_json, which is the problem in my case.

like image 598
CoMartel Avatar asked Sep 07 '17 09:09

CoMartel


1 Answers

Ok, so it turns out you can replace :

data = request.get_json()

By :

data = json.loads(request.data, strict=False) # strict = False allow for escaped char

requests.data contains the json in string format, so you can process the characters that needs to be escaped.

like image 154
CoMartel Avatar answered Sep 22 '22 06:09

CoMartel