0
votes

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
1
this error needs to be checked from the backend. if you are using a python http server, then figure out the handler class that is inherited from SimpleHTTPServer.SimpleHTTPRequestHandler. In this, print logs inside do_POST method. - user2879704
Thank you Madhavan for the help. I have figured it out. It seems that the way I was trying to post json data was not working. - Bid Sharma

1 Answers

0
votes

I solved it this way:

filehandle = open(PATH)

r = session.post(URL_IMPORT, data={},files = {'sync_config_import':filehandle})