通过表格在Flask上发布数据会给出400个错误的请求 [英] Posting Data on Flask via form is giving 400 Bad Request

查看:248
本文介绍了通过表格在Flask上发布数据会给出400个错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的前端发布数据,烧瓶应用程序正在抛出400个不好的请求。但是,如果我正在使用卷曲调用相同,似乎工作正常。

以下是我的表单代码

 <脚本> 
function sub(){
console.log('sub function');
$(#fquery)。submit();
}
< / script>
< form id =form1action =/ finalmethod =post>
< input id ='query'type =text>
< button type =submitonClick ='sub()'>提交& raquo;< / button>
< / form>

在服务器端:

  @ app.route('/ final',methods = ['POST','GET'])
def message():$ b $ if request.method =='POST ':
app.logger.debug(输入消息函数+ request.form ['query'])
q = request.form ['query']
return render_template('final。这个服务器端对我来说似乎很好,但是我觉得这个服务器端对我来说似乎很不错。因为我得到了curl请求的响应

  curl http:// localhost:8000 / final -dquery = select starct st blah blah-X POST -v 
* Trying 127.0.0.1 ... connected
> POST / gc HTTP / 1.1
> User-Agent:curl / 7.22.0(i686-pc-linux-gnu)libcurl / 7.22.0 OpenSSL / 1.0.1 zlib / 1.2.3.4 libidn / 1.23 librtmp / 2.3
>主机:localhost:8000
>接受:* / *
>内容长度:41
> Content-Type:application / x-www-form-urlencoded
>
*上传完全发送:41个字节的41个
* HTTP 1.0,假设在
之后关闭< b< HTTP / 1.0 200 OK
< Content-Type:text / html; charset = utf-8
<内容长度:1961
<服务器:Werkzeug / 0.9.4 Python / 2.7.3
< Date:Thu,24 Oct 2013 23:33:12 GMT


解决方案

<呃,我想我明白了:你只为<$ c>设置 id ,而不是名称 $ c> input 元素。然而,名称用于发送到服务器的表单数据。这会导致 KeyError request.form ['query'] 中,导致400错误。 b $ b

I am trying to post data via my front end and the flask app is throwing 400 bad request. However If I am doing the same using Curl call it seems to work fine. I dont know what I am missing in the form.

The following is my form code

<script>
function sub() {
    console.log('sub function');
    $("#fquery").submit();
}
</script>
<form id="form1" action="/final" method="post">
 <input id='query' type="text">
  <button type="submit" onClick='sub()'>Submit &raquo;</button>
</form>

At server side:

@app.route('/final',methods=['POST','GET'])
def message():
    if request.method == 'POST':
        app.logger.debug(" entered message function"+ request.form['query'])
        q = request.form['query']
    return render_template('final.html',query=q,result="Core_Table Output")

The server side seems fine to me. Since I am getting response for curl request

curl http://localhost:8000/final -d "query=select starct st blah blah" -X POST -v
*   Trying 127.0.0.1... connected
> POST /gc HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8000
> Accept: */*
> Content-Length: 41
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 41out of 41 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 1961
< Server: Werkzeug/0.9.4 Python/2.7.3
< Date: Thu, 24 Oct 2013 23:33:12 GMT

解决方案

Ah, I think I see it: You only set the id but not the name for the input element. Yet the name is used in the form data that is sent to the server. This causes a KeyError at request.form['query'] which causes the 400 error.

这篇关于通过表格在Flask上发布数据会给出400个错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆