Python Requests get __VIEWSTATE and __EVENTVALIDATION after __doPostBack

1.8k Views Asked by At

I am making requests to a website created using Asp.Net. I am using a Python Requests session to get the __VIEWSTATE and __EVENTVALIDATION variables and add them back to the data payload.

response = s.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'lxml')
viewstate = soup.find(id='__VIEWSTATE').get('value')
eventvalidation = soup.find(id='__EVENTVALIDATION').get('value')
payload.update({'__VIEWSTATE': viewstate, '__EVENTVALIDATION': eventvalidation})
session.post(url, headers=headers, data=payload)

This code works great until I do an action on the form that has an onchange of javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Chooser$Segment\',\'\')', 0). I have to perform this on a few input's so there are multiple eventTarget.

When I modify any form element that is attached to __doPostBack() function, I receive message of:

Invalid postback or callback argument

from Asp.Net.

How do I simulate multiple __doPostBack's so my __VIEWSTATE and __EVENTVALIDATION do not return an error?

1

There are 1 best solutions below

0
On

The ASP net has hidden input, which when you submit the response, you need to take all those inputs back to ASP.

So when you scrap the web, get the response, and process those contents

    def __get_hidden_input(self, content):
        """ Return the dict contain the hidden input 
        """
        tags = dict()
        soup =BeautifulSoup(content, 'html.parser')
        hidden_tags = soup.find_all('input', type='hidden')
        # print(*hidden_tags)
        for tag in hidden_tags:
            tags[tag.get('name')] = tag.get('value')
        
        return tags


Then update these dict data alone with other form data requested. Such as __EVENTTARGET, then send it back to the server.

            r=sess.get(base_frm)
            attrs = self.__get_hidden_input(r.content)
            attrs.update({'__EVNETDATA':'your_target'})
            r=sess.post(base_frm, data=attrs)