问题
I'm developing a web app in Python using WebApp2 as framework. I can't get the http POST request parameters submitted by filling a form.
That's the HTML code of the form I created
<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>
That's the result of the POST request after pressing the submit button
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
That's the code of the POST request handler
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 is webapp2.RequestHandler extended for handling sessions (i tried with webapp2.RequestHandler also and i got the same results).
What I get every time is None for both parameters.
Any suggestions about how to solve the problem? I tried self.request.get also, instead self.request.POST.get, but it didn't work too (i didn't get None neither)
回答1:
Try removing the enctype="text/plain"
attribute from the form, and then use self.request.POST.get('eMail')
and self.request.POST.get('pwd')
.
Edit: The reason why removing enctype="text/plain"
works is because you want the enctype to be "text/html"
(which is the default) in order for webapp2 to read the form as an html form. When it is just set to "text/plain"
, the form's output is contained in the body of the request as just text, which is what you saw when you printed out the request. If you use "text/plain"
, then you could access the form's output as a string by using:
form_string = str(self.request.body)
and then you could parse that string to get the key-value pairs. As you're already aware though, it is easier just to set the enctype to html to get the standard http-form functionality.
I couldn't specifically find enctype
information in the documentation, but if you have other questions about the request object I suggest reading the Webob Documentation for a request object. Webapp2 uses Webob requests, so that documentation is the place to go to understand your request obejct.
来源:https://stackoverflow.com/questions/43640970/cant-get-post-parameters