Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a form using Requests Python

So I want to automate the filling in and submitting of a form and I am using Requests to do that.

From inspecting elements I know the url to submit to and the type of submition (post):

method="post"
action="/sformsubmit"
enctype="multipart/form-data"

My problem is that my request is not going through and being fairly new to this I am not sure why.

On my webpage I have two buttons side by side like so :

   ___________________________          ________________________________
   |    Submit decleration   |          |        Reset Form            |
   ___________________________          ________________________________

And when I inspect elements on that line i get:

<td align="center" colspan="2">
    <input type="hidden" name="inLeader" value>
    <input type="hidden" name="inMember" value>
    <input type="hidden" name="version" value="0">
    <input type="hidden" name="key" value="2013:a:c:3:2s">
    <input type="submit" value="Submit declaration">
    <input type="reset" value="Reset form">
</td>

I am trying the following:

>>> payload = {'inLeader':'', 'inMember':'', 'version':'0', 'key':'2013:a:c:3:2s'}

However it doesnt seem to work and isnt generating any errors.

Any help would be grateful. Thanks in advance

like image 977
user2897415 Avatar asked Oct 19 '13 10:10

user2897415


People also ask

How do you submit data to a form in Python?

To post HTML form data to the server in URL-encoded format using Python, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the Python POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How do you send data in a Python POST request?

To send a POST request using the Python Requests Library, you should call the requests. post() method and pass the target URL as the first parameter and the POST data with the data= parameter.

What does requests get () do?

The get() method sends a GET request to the specified url.


1 Answers

Okay, your payload is wrong. BUT. I'm not sure if changing it will actually help because you didn't include the error message you are receiving.

payload = {
    'inLeader':'',
    'inMember':'',
    'version':'0',
    'key':'2013:a:c:3:2s',
}

What you need to understand about HTML forms and POST requests is that when you click a submit button on a form it sends the value attribute of any field with a name attribute. The input field with the type submit doesn't get sent for example. It has no name. I'm suspicious that the inLeader and inMember fields have no data. Is this being set via Javascript somehow?

You mention in a comment that you need to be logged in order to access the form? This most likely means that you also need to send the correct cookie along with the request. So, visiting the URL I get asked for a username/password. This website is using basic auth.

requests supports this. Example below:

import requests
from requests.auth import HTTPBasicAuth
requests.get(url, auth=HTTPBasicAuth('your username', 'your password'))

Try just making a get request and seeing if you can at least get a 200 response. That means that the auth is working. Then you can try and do the actual post.

like image 101
aychedee Avatar answered Oct 17 '22 12:10

aychedee