Introduction
使用flask搭建了一个小小服务器,在做unittest时,使用GET方法请求对应接口,结果没有都是400
.
- server code
@app.route("/download")
def downloadFeedbackFile():
print request.form["name"]
return "hello"
GET command
curl '127.0.0.1:5010/download?name=hello'
Return Result
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
Reason
傻x的我对flask form
不太了解了,form表单都是POST
提交对而不是GET
参数, 所以flask没有都在那里执行
失败,“可爱的400”就与我见面了。Flask Form
Things to remember:
create the form from the request form value if the data is submitted via the HTTP POST method and args if the data is submitted as GET.
Resolve
知道了原因解决方案就迎刃而解了
@app.route("/download")
def downloadFeedbackFile():
print request.args.get("name")
return "hello"
神奇的200终于出来了HTTP/1.0 200 OK
Tips
- @jingfeng 提出这个问题
- 对新的东西还是多了解源文档,对使用对属性、方法有较深对理解,就会避免这样的傻x问题
- 解决问题时已经不要着急,着急是解决问题的魔鬼