美文网首页
flask server occur 400 at reques

flask server occur 400 at reques

作者: frank3 | 来源:发表于2018-01-22 10:14 被阅读0次

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问题
  • 解决问题时已经不要着急,着急是解决问题的魔鬼

相关文章

网友评论

      本文标题:flask server occur 400 at reques

      本文链接:https://www.haomeiwen.com/subject/zuxlaxtx.html