0
votes

step1: I'm grabbing credentials from a text file where data is in JSON format and storing them in a variable.

cred_values = {'username': 'myuser', 'password': 'mypwd'}

Step2:

username = cred_values['username']

password = cred_values['password']

Step3: Preparing my payload, headers. And payload looks like this

login_headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache',
    'Origin': 'https://xxxxxx.com.au', 'Upgrade-Insecure-Requests': '1',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9',
}

login_data = {
    'curl': 'Z2FxxxxxZ2F', (xxxx - name of my company)
    'flags': '0',
    'forcedownlevel': '0',
    'formdir': '5',
    'username': username,
    'password': password,
    'trusted': '4',
    'SubmitCreds': ''
}

Step 4: Post request

 login_request_url = 'https://xxxx.com.au/Logon'
 login_response = requests.post(login_request_url, headers=login_headers, data=login_data)

Note:

  1. I also tried sending payload as

    login_data = {'username': '' + username + '','password': '' + password + ''}
    
    login_data = {'username': '' + str(username) + '','password': '' + str(password) + ''}
    
  2. I also tried sending payload as json.dumps to the request

    login_response = requests.post(login_request_url, headers=login_headers, data=json.dumps(login_data))
    

I'm not getting any errors if I post the above request its not logging in.

Ex:

If I directly add my username, pwd in login_data

The Url looks like this, which means successfully logged in - 'https://xxxx.com.au/content.asp?token = xxxxx'

If I send username and pwd by grabbing from credential file

The Url looks like this which means NOT successfully logged in - 'https://xxxx.com.au/'

1
What goes wrong? Is your server getting the wrong message or no message at all? Or do you get an error?Samie Bencherif
This can't be your real code, because login_headers is undefined. Please post your real code.John Gordon
It will be a mystery if you don't add the url and other details. Because i don't know what occurred.KC.
If you're sending json data use json= instead of data=pale bone
Posting using json = didnt work. it is returning 400 responseak veer

1 Answers

0
votes

Depending on what your server is expecting you may possibly need to send the json payload as a quoted string: I think you had the following:

login_data = {'username': '' + username + '','password': '' + password + ''}
login_data = {'username': '' + str(username) + '','password': '' + str(password) + ''}

Try the following

login_data =  '{"username": " ' + str(username) + ' ","password": "' + str(password) + '"}'

Use the single quotes to go from code to string, and use the double quotes to denote a quoted value within the string (or you could do it the other way). Alternatively you could just use the same set of quotes through out and escape the ones that you want with in the string, but personally I think that get's very hard to read very quickly e.g.

data =  '{\'username\': \''+ str(username) + '\',\'password\': \'' + str(password) + '\'}'

See if that helps