flask server occur 400 at request.form("arg") using GET Method


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问题
  • 解决问题时已经不要着急,着急是解决问题的魔鬼
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容