烧瓶request.get_json()引发BadRequest [英] Flask request.get_json() raise BadRequest

查看:46
本文介绍了烧瓶request.get_json()引发BadRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有此端点的Flask API:

I have a Flask API with this endpoint :

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

当我使用python发布请求时,eveything很好.

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

但是当我使用Postman时,我得到了:

But when I use Postman, I get :

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

我同时发送了相同的Json(请确保将其复制/粘贴).我相当有信心,这个问题是由json中的某些"\ t"引起的,这些错误由python转义了,而Postman则没有.

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.

是否可以在应用程序中检索原始json并进行处理(转义需要转义的内容)?还是获取json的另一种方法?

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 ?

这是与您建议重复的问题不同的问题,因为您的建议使用get_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.

推荐答案

好吧,事实证明您可以替换:

Ok, so it turns out you can replace :

data = request.get_json()

通过:

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

requests.data包含字符串格式的json,因此您可以处理需要转义的字符.

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

这篇关于烧瓶request.get_json()引发BadRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆