Skip to content Skip to sidebar Skip to footer

Python Requests - Inserting Credentials As Variables In Form_data

I'm trying to prompt the user for the username and password to login to a site. My plan is to use requests to send the POST data. Here is the code I have so far: import requests im

Solution 1:

POST data from a form is just a series of key-value pairs. Because all the keys are unique, just use a dictionary to represent these:

FORM_DATA = {
    '__EVENTTARGET': '',
    '__EVENTARGUMENT': '',
    '__VIEWSTATE': '/wEPDwUKMTA5NTA5ODU1MQ9kFgJmD2QWAgIGDxBkDxYFZgIBAgICAwIEF***REMAINDER REMOVED***',
    '__EVENTVALIDATION': '/wEdAAp4d3BHvSTs+Kv6cxGP3xEbBr8xrgRYad2tj4YCyRIw5qUAjimf****REMAINDER REMOVED****',
    'jsCheck': '',
    'ddlEngine': 'REMOVED:13008',
    'Username': uname,
    'Password': passw,
    'btnLogin.x': '42',
    'btnLogin.y': '9',
    'btnLogin': 'Login',
}

with empty values represented by empty strings.

The btnLogin.x and btnLogin.y keys are usually just ignored by a server; they communicate where you clicked on an image button. You can just set the username and password keys directly from the uname and passw variables you already set.

Then use this dictionary as the data keyword to the requests.post() call.

It may be the server expects a cookie back, set from the initial loading of the form. In that case a Session object and call .get() and .post() on that.

It may be that the __VIEWSTATE and __EVENTVALIDATION keys are dynamically set when the form is first generated, you would need to load it with requests.get(), parse the HTML (using, say, BeautifulSoup) and extract the form fields before sending.

Post a Comment for "Python Requests - Inserting Credentials As Variables In Form_data"