The code below tries to upload json data which will be handled at backend by the url "localhost:5000/import_loginspect_data". The status code prints 400 i.e bad request. I printed the header and looks as below:
"" {'date': 'Thu, 27 Nov 2014 03:14:10 GMT', 'set-cookie': 'session=.eJxtUduOmzAQ_RU0z2jFJQjCU6WN2t2HIFVKVTVVZTl4ADcGU1827a723zsG8lCpb8y5zBwf3oCLUU5QvwFvnXxBqJ3xGAOOXCqoYeRXrvyHRfXQ6hFi6CQqYYNlkELgxO7A9x8x2EHfGFdq3fMe1Ma6iY-0GYLZK7VN6-UYFLdO6X5NsXwwwR2GKRKkOw0-jrIyavRLlCXpLkqqutjXaRJ9Op7gnW4owWdmnSFX_2e9Mxvs0ODU4pK0N_qm2KytdFLTIbgYEg3aG9ZpM3JHULaLnggg3CI37cAGVDN5Zj9D3XFlMTDOyam3C3fvykvmBlzeJLDjXjmgUL88mi2MxdagY1cMcynSQpTZvmovVZGLtNxjlQvMM5FmRZl1pHdSrEYnR2SvelrKEGhbI-ftAV9Oj9GJ6HNgYwiOvIxh65bYkMFbNIGhJi-ENqfPWfP1-Xdz-HY7Hj5ej6_Pt-Znn5_H83A89Pnd8s8PIkxO2lND6S4tk6JKimSLuK39v-0v-2PCsg.B1gogg.Z2qcfGZt_kWX5_pW8uhb8bFx5zg; HttpOnly; Path=/', 'content-length': '192', 'content-type': 'text/html', 'server': 'Werkzeug/0.9.6 Python/2.7.5'}
"" I am new to requests api, so any help will be appreciated.Thanks :)
def import_sync(USER,PASSWORD,PATH):
with open(PATH) as json_file:
json_data = json.load(json_file)
# Start a session so we can have persistant cookies
session = requests.session(config={'verbose': sys.stderr})
# This is the form data that the page sends when logging in
login_data = {
'username': USER,
'password': PASSWORD,
'submit': 'login',
'url_hash' :'/'
}
headers = {
'Content-type': 'application/json',
'Accept': 'application/json'
}
data = {
'sync_config_import':json.dumps(json_data)
}
# Authenticate
r = session.post(URL, data=login_data)
# Try accessing a page that requires you to be logged in
r = session.post('http://localhost:5000/import_loginspect_data',data = data,headers = headers)
header = r.headers
print header
print r.status_code