繁体   English   中英

无法获取POST参数

[英]Can't get POST parameters

我正在使用WebApp2作为框架在Python中开发Web应用程序。 我无法通过填写表单来获取提交的http POST请求参数。

这是我创建的表单的HTML代码

<html>
<head>
<title>Normal Login Page </title>
</head>
<body>
<form method="post" action="/loginN/" enctype="text/plain" >
eMail: <input type="text" name="eMail"><br/>
password: <input type="text" name="pwd"><br/>
<input type="submit">
</form>
</body>

这是按下提交按钮后POST请求的结果

POST /loginN/ HTTP/1.1
Accept: 
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control: max-age=0
Content-Length: 33
Content-Type: text/plain
Content_Length: 33
Content_Type: text/plain
Cookie: 
session=############
Host: ###########
Origin: ###########
Referer: ############
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
X-Appengine-City: #######
X-Appengine-Citylatlong: ########
X-Appengine-Country: ##
X-Appengine-Region: ##
X-Cloud-Trace-Context: ##

eMail=mymail@email.com
pwd=mypwd

这是POST请求处理程序的代码

class loginN(BaseHandler):
    def post(self):
        w = self.response.write
        self.response.headers['Content-Type'] = 'text/html'
        logging.info(self.request)
        logging.info(self.request.POST.get('eMail'))
        logging.info(self.request.POST.get('pwd'))
        email = self.request.POST.get('eMail')
        pwd = self.request.POST.get('pwd')
        w('<html>')
        w('<head>')
        w('<title>Data Page </title>')
        w('</head>')
        w('<p>Welcome! Your mail is: %s</p>' % email)
        w('<p>Your pwd is: %s</p>' % pwd)
        w('</body>')  

BaseHandler是webapp2.RequestHandler扩展用于处理会话(我尝试使用webapp2.RequestHandler也得到了相同的结果)。

对于这两个参数,我每次得到的都是None。

有关如何解决问题的任何建议? 我也尝试了self.request.get,而不是self.request.POST.get,但它也没有用(我没有得到None)

尝试从表单中删除enctype="text/plain"属性,然后使用self.request.POST.get('eMail')self.request.POST.get('pwd')

编辑:删除enctype="text/plain"的原因是因为您希望enctype为"text/html" (这是默认值),以便webapp2将表单作为html表单读取。 当它只是设置为"text/plain" ,表单的输出将作为文本包含在请求正文中,这是您在打印请求时看到的内容。 如果使用"text/plain" ,则可以使用以下方法将表单的输出作为字符串访问:

form_string = str(self.request.body)

然后你可以解析该字符串以获得键值对。 正如您已经知道的那样,将enctype设置为html以获得标准的http-form功能更容易。

我无法在文档中专门找到enctype信息,但如果您对请求对象有其他疑问,我建议您阅读请求对象的Webob文档 Webapp2使用Webob请求,因此文档是理解您的请求obejct的地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM