I have a very vague question that I'm not sure where to start from.
I need to make an ajax login using python (django REST calls)
jQuery is available from my end.
I was given this:
import sys, base64
import httplib, urllib, urlparse, json
auth = 'Basic %s' % base64.encodestring('%s:%s' % (username, password)).strip()
headers = {'Content-Type':'application/x-www-form-urlencoded', 'Authorization':auth}
endpoint = "urlendpoint.com"
url = "/login/"
conn = httplib.HTTPSConnection(endpoint)
conn.request("POST", url, "", headers)
response = conn.getresponse()
if response.status != httplib.OK: raise Exception("%s %s" % (response.status, response.reason))
result = json.load(response)
if "token" not in result: raise Exception("Unable to acquire token.")
if "sessionid" not in result: raise Exception("Unable to acquire session ID.")
print result["token"], result["sessionid"]
conn.close()
I need to send the login via POST and then set a cookie.
I have absolutely no clue where to begin with this task. Using the command line, I'm able to set up a /login.py file, access said file with a username and password hard-coded in the above variable field and VIOLA - it works fine. However, I have no clue where to begin using ajax for this task.
The main thing here is that I need to establish a session ID between the logged in user and the server once the person has logged in, in order to get access to the REST (json) endpoints so that I can start adding data (via Ajax).
Any assistance would be much appreciated.
I'm hoping that someone