2

I have a simple html form (postform.html) which takes in two user inputs and passes them to a php file (post.php). The php file then echoes a string containing the user inputs.

I am running this from a local host and the code for the form is shown below:

<form action = "post.php" method = "post">
    Name <input type = "text" name = "user"><br />
    Company <input type = "text" name = "comp"> <br />
    <input type = "submit" value = "Submit Info">
</form>

I would like to, using the Python requests module, post information into postform.html, and then retrieve the source code for the resulting echo. I have tried the following in Python command prompt after importing the requests module:

>>> data = {'user': 'Bob', 'comp': 'Walmart'}
>>> r = requests.post("http://localhost/practice/SimpleForm/postform.html",data)
>>> print(r.text)
<html>
<head><title> Post Method in Action </title></head>
<body>
    <form action = "post.php" method = "post">
        Name <input type = "text" name = "user"><br />
        Company <input type = "text" name = "comp"> <br />
        <input type = "submit" value = "Submit Info">
    </form>
</body>
<html>

As you can see the text returned by the variable is the original html containing the form which is not what I want, I am trying to post information to the form and retrieve the source html for the result (post.php).

In some ways my question is similar to Python: How to send POST request? but not the same since this is a much simpler and more straightforward example of how to apply the requests.post function in Python. I have tried implementing the solution in the above thread but again only got the text for the original html.

Community
  • 1
  • 1
user32882
  • 5,094
  • 5
  • 43
  • 82

1 Answers1

3

Seems like you've got an issue in your Post request:

>>> r = requests.post("http://localhost/practice/SimpleForm/postform.html",data)
                                                                     ^^^^

The form is trying to point to postform.php, and not an html file. Simply change it to this:

>>> r = requests.post("http://localhost/practice/SimpleForm/post.php",data)
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • Thanks. That worked. Would similar rules apply to an aspnet form? – user32882 Jan 12 '16 at 01:47
  • @user32882 Yep, they would. The reason is because it's point to a url, and even a mistake in the ending of a url can cause errors such as a 404. Generally, you need to look at the actual html form, to determine where it will go once completed. – Zizouz212 Jan 12 '16 at 01:49