python的request返回400,如何故意在Python / Flask中引起400错误请求?

A consumer of my REST API says that on occasion I am returning a 400 Bad Request - The request sent by the client was syntactically incorrect. error.

My application (Python/Flask) logs don't seem to be capturing this, and neither do my webserver/Nginx logs.

Edit: I would like to try to cause a 400 bad request in Flask for debugging purposes. How can I do this?

Following James advice, I added something similar to the following:

@app.route('/badrequest400')

def bad_request():

return abort(400)

When I call this, flask returns the following HTML, which doesn't use the "The request sent by the client was syntactically incorrect" line:

400 Bad Request

Bad Request

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

(I'm not sure why it isn't including the

tags.

It appears to me that there are different variations of the 400 error message. For example, if I set a cookie to a value of length 50,000 (using Interceptor with Postman), I'll get the following error from Flask instead:

Bad Request

Bad Request

Error parsing headers: 'limit request headers fields size'

Is there a way to get Flask to through the different variations of 400 errors?

解决方案

You can use abort to raise an HTTP error by status code.

from flask import abort

@app.route('/badrequest400')

def bad_request():

abort(400)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值