【问题标题】:flask request args parser error The browser (or proxy) sent a request that this server could not understandflask request args parser error 浏览器(或代理)发送了一个该服务器无法理解的请求
【发布时间】:2018-04-27 08:13:36
【问题描述】:

使用 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

当从标题中删除'content-type': 'application/json'它可以工作

我不理解这种行为,如果没有不优雅的try, expect,我该如何防范它。

感谢您的帮助

【问题讨论】:

    标签: python flask flask-restful


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2020-06-11
      • 2020-03-19
      • 2018-07-24
      • 2020-10-17
      • 2021-12-04
      • 2021-06-04
      • 2018-06-21
      • 2021-07-04
      相关资源
      最近更新 更多