flask request args parser error 浏览器(或代理)发送了该服务器无法理解的请求

flask request args parser error The browser (or proxy) sent a request that this server could not understand

使用 test_client 并像这样发送请求:

app = Flask(__name__)
client = app.test_client()

headers = {'content-type': 'application/json', 'Authorization': u'Bearer fake_token_123'}
params = {'dont_care': True}
client.get(ֿֿ'/my_route/123', query_string=params, headers=headers)

我的路线是

class MyController(Resource):

    def get(self, id):
        parser = reqparse.RequestParser()
        parser.add_argument('currency', type=str, default='USD', help="help text")

        args = parser.parse_args()
        currency_from_params = args.currency

parser.parse_args() 失败

The browser (or proxy) sent a request that this server could not understand

从 header 中删除 'content-type': 'application/json' 有效

我不明白这种行为,如果没有优雅 try, expect,我该如何防止这种行为。

感谢您的帮助

您已经找到了解决方法:如果您不发布 JSON,请不要发送 content-type: application/json。你不能用 GET 发送 JSON,即使你可以(或使用 POST)你也必须先用 json.dumps(data) 编码 JSON。