Search code examples
pythonjsonurllib2

How to use python urllib2 to send json data for login


I want to use python urllib2 to simulate a login action, I use Fiddler to catch the packets and got that the login action is just an ajax request and the username and password is sent as json data, but I have no idea how to use urllib2 to send json data, help...


Solution

  • import urllib2
    import json
    # Whatever structure you need to send goes here:
    jdata = json.dumps({"username":"...", "password":"..."})
    urllib2.urlopen("http://www.example.com/", jdata)
    

    This assumes you're using HTTP POST to send a simple json object with username and password.