GET and POST Method Issues in Python

File name mypage.py

Python code

form = cgi.FieldStorage() name = form.getfirst('name', 'empty') id = form.getfirst('id', 'empty') print """\ Content-Type: text/html\n <html> <body> <p>Name: "%s"</p> <p>ID: "%s"</p> </body> </html> """ % (name, id) 

HTML inside the same file

 <form name="frm" method="post" action="mypage.py?id=33"> <input type="text" name="name" value="MyName" /> <input type="Submit" id="btn" value="Send" /> 

After submitting the form (click "Submit"), I see this URL with the following output

  localhost:8000/cgi-bin/mypage.py?id=33 Name: "empty" ID: "33" 

if I change the POST form method to GET

 <form name="frm" method="get" action="mypage.py?id=33"> 

then I see this URL with the following output

 localhost:8000/cgi-bin/mypage.py?name=MyName Name: "MyName" ID: "empty" 

I do not understand why I am not getting the value of the text field using the POST method? And why can't I get the id value in the query string using the GET method?

Its a simple python page without any frameworks. BTW I use "python-bugzilla 0.8.0", downloaded from here , but I think my given code is just a simple page and has nothing to do with this package.

Any help would be greatly appreciated.

Thanks in advance,

+4
source share
4 answers

Thanks for the help.

the problem was here.

 form = cgi.FieldStorage() 

As I mentioned in your answers, I typed in a โ€œform,โ€ and it says โ€œFieldStorage (None, None, []).โ€

So, if FieldStorage doesn't matter, then it doesn't matter which function is used to get the form value. But it was really good information and practical.

previously, form = cgi.FieldStorage() was declared inside another function that was incorrect, so FieldStorage was empty.

Here is the WRONG code.

 def myfunction (): cgitb.enable() form = cgi.FieldStorage() 

Solution 1:

form = cgi.FieldStorage() must define run () inside the function and pass this form as a parameter to another function to get the values โ€‹โ€‹of the form.
those.

 def run(): cgitb.enable() form = cgi.FieldStorage() myfunction(form) 

Now it works

 def myfunction (form): name = form.getfirst('name', 'empty') id = form.getfirst('id', 'empty') 

Solution 2:

form = cgi.FieldStorage() must be defined directly inside the main function, then you do not need to pass it as a parameter.
those.

 if __name__ == "__main__": cgitb.enable() form = cgi.FieldStorage() 

Now its work and form are available inside my function.

 def myfunction (): name = form.getfirst('name', 'empty') id = form.getfirst('id', 'empty') 

Thanks to everyone.

+1
source

Your GET is correct:

 <form name="frm" method="get" action="mypage.py?id=33"> 

But your POST is not:

 <form name="frm" method="post" action="mypage.py?id=33"> 

You cannot add a GET style variable (? Id = 33) to your POST action. It should be:

 <form name="frm" method="post" action="mypage.py"> <input type="hidden" name="id" value="33"> 

See HTTP Methods: GET vs. POST : "Request strings (name / value pairs) are sent to the GET request URL" and "Request strings (name / value pairs) are sent in the body of the HTTP POST request message".

Failure to comply with these rules will lead to unexpected results that you see.

+1
source

Technically, the POST destination URL should not have GET parameters, so ?id=33 is not valid in the target case. I also assume this confuses the FieldStorage module, which is probably why you are getting unexpected results.

+1
source

You must correctly use POST and GET for my other answer. However, I am worried that you use the names form.getfirst and variable.

In the documentation :

FieldStorage.getfirst (name [, default]) - This method always returns only one value associated with the form field name . The method returns only the first value in case more values โ€‹โ€‹were published under that name .

You named the name of a variable name, which is a stupid name. Look, a lot of names. And your form has a name. And this is the field. Same thing with id. You should change the variable names as such:

 <form name="MyForm" method="post" action="mypage.py"> <input type="text" name="FullName" value="MyName" /> <input type="text" name="FormID" value="33" /> <input type="Submit" id="btn" value="Send" /> 

and change your Python as follows:

 form = cgi.FieldStorage() FullName = form.getfirst('FullName', 'empty') FormID = form.getfirst('FormID', 'empty') print """\ Content-Type: text/html\n <html> <body> <p>Name: "%s"</p> <p>ID: "%s"</p> </body> </html> """ % (FullName, FormID) 

This is the code for correct POST and printing variables. It works?

+1
source

Source: https://habr.com/ru/post/1499662/


All Articles