Having trouble submitting a login form with Python-Requests (200 Reponse but Expecting 302)

Viewed 47

I'm trying to log-in to a website using Python requests. My setup is:

headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'content-type': 'application/x-www-form-urlencoded',
'origin': 'https://www.naturalpartners.com',
'referer':'https://www.naturalpartners.com/us/account/login.jsp',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}

payload = {'_dyncharset': 'UTF-8',
'_dynSessConf': '',
'/atg/userprofiling/ProfileFormHandler.flowName': 'B2B',
'_D:/atg/userprofiling/ProfileFormHandler.flowName': ' ',
'/atg/userprofiling/ProfileFormHandler.loginSuccessURL': '/us/index.jsp?',
'_D:/atg/userprofiling/ProfileFormHandler.loginSuccessURL': ' ',
'/atg/userprofiling/ProfileFormHandler.loginErrorURL': '/us/account/login.jsp?',
'_D:/atg/userprofiling/ProfileFormHandler.loginErrorURL': ' ',
'/atg/userprofiling/ProfileFormHandler.value.login': '__UsernameHidden__',
'_D:/atg/userprofiling/ProfileFormHandler.value.login': ' ',
'/atg/userprofiling/ProfileFormHandler.value.password': '__PasswordHidden__',
'_D:/atg/userprofiling/ProfileFormHandler.value.password': ' ',
'/atg/userprofiling/ProfileFormHandler.login': 'Sign In',
'_D:/atg/userprofiling/ProfileFormHandler.value.login': ' ',
'_DARGS': '/us/account/login.jsp'}

My code to execute the login is:

from requests_html import HTMLSession

session = HTMLSession() #new session
url = 'https://www.naturalpartners.com/us/account/login.jsp?_DARGS=/us/account/login.jsp'
r = session.get('https://www.naturalpartners.com/us/account/login.jsp', headers = headers) #get login page
dyn = r.html.xpath('//input[@name="_dynSessConf"]/@value')[0] #store _dynSessConf
payload['_dynSessConf'] = dyn #assign to payload
headers['cookie'] = r.headers['Set-Cookie'] #attempt to set cookie value
post = session.post(url, data = payload, headers=headers)

My issue is I'm getting a 200 response as opposed to a 302 which would signal a successful login. The post seems to be completing the form with the username and password but not actually submitting it. So the login doesn't actually take place. I'm not sure what step I'm missing. Thank you

0 Answers
Related