Submitting a form using Python requests

So, I want to automate the completion and submission of the form, and I use Requests for this.

From checking the items, I know the URL to send and the type of feed (message):

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

My problem is that my request does not go through, and I'm pretty new to this. I do not know why.

On my webpage, I have two buttons next to each other:

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

And when I check the elements on this 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 to do the following:

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

However, it does not seem to work and does not generate any errors.

Any help would be greatly appreciated. thanks in advance

+6
source share
1 answer

Well, your payload is incorrect. BUT. I'm not sure that if you change this, it will really help, because you did not indicate the error message that you receive.

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

What you need to know about HTML forms and POST requests is that when you click the submit button on the form, it sends the value attribute of any field with the name attribute. An input field of type submit not sent, for example. It does not matter name . I suspiciously the inLeader and inMember have no data. Is this installed through Javascript?

Did you mention in the comment that you need to be logged in to access the form? This most likely means that you also need to send the correct cookie along with the request. So, having visited the URL, I will ask you to enter a username / password. This website uses basic auth .

requests supports this. Example below:

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

Just try to get a request for a receipt and see if you can at least get a 200 response. That means auth is working. Then you can try and make the actual post.

+5
source

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


All Articles