Python Flask未从请求中获取任何POST数据

Python Flask未从请求中获取任何POST数据,python,json,flask,http-post,Python,Json,Flask,Http Post,在我们的服务器中,有一段代码调用我的应用程序中的函数,如下所示: 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" : "

在我们的服务器中,有一段代码调用我的应用程序中的函数,如下所示:

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()
@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
dat = dict( api_key="a-key-goes-here" )
request = requests.post(url, data=dat)
message = request.text
code = request.status_code
我知道这不是使用url_(也不是通过应用程序调用url的其他方式),但这是有原因的。我们的服务器正在测试调用是否正确,但url预期在我们的应用程序之外,api密钥也是如此

因此,我们的服务器处理url如下所示:

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()
@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
dat = dict( api_key="a-key-goes-here" )
request = requests.post(url, data=dat)
message = request.text
code = request.status_code
我们的错误如标题所示:

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

我有没有做错什么,或者以其他方式将数据从烧瓶发布到外部

如果需要更多我愿意更新的信息,就打电话给它

编辑1 我转而使用如下请求:

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()
@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
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) INFO This is message ImmutableMultiDict([]) ImmutableMultiDict([])  and this code 403
request.args-request.form-request.data,它们都是空的,使用urllib或requests

关于编辑1的更新 从Martin konecny建议的方法中删除“GET”后,我得到了以下响应:

(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>
(08/02/2014 08:35:06 AM)信息这是一条信息
405方法不允许
方法不允许
请求的URL不允许使用该方法

这个代码是405

但换句话说,request.form是空的,它不应该是空的

我不认为你能推断出这个结论。似乎发生的是你有一个带有POST标题的GET请求

从:

数据可以是指定要发送到服务器的其他数据的字符串,如果不需要此类数据,则可以是无数据。目前,HTTP请求是唯一使用数据的请求;提供数据参数时,HTTP请求将是POST而不是GET。数据应是标准应用程序/x-www-form-urlencoded格式的缓冲区。urllib.urle函数的作用是:获取一个2元组的映射或序列,并以这种格式返回一个字符串

您需要删除标题

“内容类型”:“application/x-www-form-urlencoded;charset=UTF-8”


当您发送带有空数据结构的请求时,或者只是将其全部删除(需要时,
urllib
会自动添加该请求)

2件事。首先,在使用URL之前将其打印出来,以确保它与Flask中的URL匹配。其次,您考虑过使用吗?它真的很棒。请检查编辑。我不得不将我的请求从POST切换到get,因为我不知道为什么要继续使用urlopen(URL,数据)它失败了。我已经按照你说的做了,删除了内容类型,并对dict进行了URL编码,因此我不知道帖子失败的原因。你的新请求方法看起来是正确的。删除
methods='GET'
以确保你只处理帖子(现在),然后打印
request.form
的内容。你保留
methods=[“POST”]
?另外,请先在不使用api_键的情况下进行尝试。一旦成功,请添加。我保留了POST方法