Flask没有从请求中获取任何POST数据 [英] Flask is not getting any POST data from a request

查看:2303
本文介绍了Flask没有从请求中获取任何POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的服务器中,我们已经得到了这个代码调用我的应用程序中的一个函数,如下所示:

  data = urllib .urlencode(api_key =a-key-goes-here))
headers = {
User-Agent:Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.36 KHTML,像Gecko)Chrome / 29.0.1547.2 Safari / 537.36,
接受:text / xml,application / xml,application / xhtml + xml,text / html; q = 0.9,text / plain; q = 0.8,text / png,* / *; q = 0.5,
Accept-Language:en-US,en; q = 0.8,
Accept-Charset ISO-8859-1,utf-8,
Content-type:application / x-www-form-urlencoded; charset = UTF-8
}
request = urllib2 。Request(url,data,headers)
response = urllib2.urlopen(request)$ b $ code = response.code $ b $ message = response.read()
response.close()

我知道这不是使用url_for(也没有其他方法通过您的APP调用url,有一个原因,我们的服务器只是测试呼叫是否正确但是网址预计会在我们的APP之外,所以是API密钥。所以,我们的服务器处理网址如下所示:



$ b

b

  @ app.route('/ loan_performer',methods = [GET,POST])
def loan_performer():
if request.form和request.form ['api_key'] == API_KEY:
ret = dict()

#rate1中的'api_key'返回一个介于3.000和4.000之间的随机数point1将是0
ret ['rate_one'] = random.randint(3000,4000)
ret ['point_one'] = 0

#rate2在3.500和4.500,点2是0.5
ret ['rate_two'] = random.randint(3500,4500)
ret ['point_two'] = 0.5

#rate3 between 4.000 and 5.000 with 1.0
ret ['rate_three'] = random.randint(4000,5000)
ret ['point_three'] = 1.0

return json.dumps(ret), 200

其他:
返回u你的AP我的密码是无效的。,403

我们的错误是因为标题说:



我们不断收到错误错误的请求(GET和HEAD请求可能不包含请求正文)这是Passenger WSGI为Apache处理的404错误。但换句话说,由于request.form是空的,它不应该。

有什么我做错了或任何其他方式POST数据Flask to outside?

如果需要更多的信息,我愿意更新,只需调用它。



编辑1



我转而使用这样的请求:

  dat = dict(api_key =a-key-goes-here)
request = requests.post(url,data = dat)
message = request.text
code = request.status_code

它拒绝工作。这是我在处理url函数中的记录器:

 (08/02/2014 07:07:20 AM)信息这是消息ImmutableMultiDict([])ImmutableMultiDict([])和这个代码403 

request.args - request.form - request.data,它们都是空的,使用urllib或请求。

更新编辑1



从Martin konecny建议的方法中删除GET之后,我得到了这个回应:

 (08/02/2014 08:35:06 AM)信息这是<!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 3.2 Final // EN> 
< title> 405方法不允许< / title>
< h1>方法不允许< / h1>
< p>请求的网址不允许使用此方法。< / p>

和这段代码405

解决方案


但换句话说,由于某种原因,request.form是空的,它不应该是这样。

我不认为你可以推断出这个结论。看来正在发生的是你有一个POST头的GET请求。



您需要删除标题


Content-type:application / x-www-form-urlencoded; charset = UTF-8当你发送一个空的数据结构的请求,或者把它全部删除(它会自动添加

urllib 需要的时候)


Within our server we've got this piece a code calling a function inside my APP like this:

data = urllib.urlencode( dict(api_key="a-key-goes-here") )
headers = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36",
    "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
    "Accept-Language" : "en-US,en;q=0.8",
    "Accept-Charset" : "ISO-8859-1,utf-8",
    "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
}
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
code = response.code
message = response.read()
response.close()

I know that this is not using url_for( neither other ways to call a url trough your APP but this has a reason. Our server is just testing that the call goes correctly but the url is expected to be outside our APP and so is the api key.

So, our server handling url looks like this:

@app.route('/loan_performer', methods=["GET", "POST"])
def loan_performer():
    if 'api_key' in request.form and request.form['api_key'] == API_KEY:
        ret = dict()

        # rate1 return a random number between 3.000 and 4.000 and point1 will be 0
        ret['rate_one'] = random.randint(3000, 4000)
        ret['point_one'] = 0

        # rate2 do it between 3.500 and 4.500, point2 being 0.5
        ret['rate_two'] = random.randint(3500, 4500)
        ret['point_two'] = 0.5  

        # rate3 between 4.000 and 5.000 with 1.0
        ret['rate_three'] = random.randint(4000, 5000)
        ret['point_three'] = 1.0

        return json.dumps(ret), 200

    else:
        return u"Your API Key is invalid.", 403

Our error is as the title says:

We are constantly receiving the error "Bad request (GET and HEAD requests may not contain a request body)" Which is a 404 error handled by Passenger WSGI for Apache. But in other words, for a reason request.form is empty and it shouldn't.

Is there anything I'm doing wrong or any other way to POST data from Flask to outside?

If there is needed more info I'm willing to update, just call it.

Edit 1

I switched to use requests like this:

dat = dict( api_key="a-key-goes-here" )
request = requests.post(url, data=dat)
message = request.text
code = request.status_code

And it refuses to work. This is the logger I made in the handling url function:

(08/02/2014 07:07:20 AM) INFO This is message ImmutableMultiDict([]) ImmutableMultiDict([])  and this code 403

request.args - request.form - request.data , all of them are empty, either using urllib or requests.

Update on Edit 1

After removing "GET" from methods suggested by Martin konecny I got this response:

(08/02/2014 08:35:06 AM) INFO This is message <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

and this code 405

解决方案

But in other words, for a reason request.form is empty and it shouldn't.

I don't think you can infer this conclusion. What appears to be happening is you have a GET request with a POST header.

From the Python docs:

data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.

You need to remove the header

"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"

when you are sending requests with an empty data structure, or just remove it alltogether (it will be added automatically by urllib when needed)

这篇关于Flask没有从请求中获取任何POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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