Post xml data with auth using python
Question: I have this xml string i want to POST to an API url, i've been cheking the
docs and came up with something like this:

    
    
    import urllib.request as ur
    import urllib.parse as up
    
    auth_handler = ur.HTTPBasicAuthHandler()
    auth_handler.add_password(realm='something',
                            uri='http://api/api',
                            user=username,
                            passwd=passw)
    
    opener = ur.build_opener(auth_handler)
    opener.addheaders = [('User-agent', 'api-id'), ("Content-Type","applicaiton/xml;charset=utf-8")]
    
    data = up.urlencode(('<?xml version="1.0" encoding="UTF-8"?>'
                    "<entry>"
                            "<episode>"+ep_no+"</episode>"
                            "<status></status>"
                            "<score></score>"
                            "<tags></tags>"
                    "</entry>"))
    
    bin_data = data.encode('utf-8')
    opener.open("http://api/api/add/"+id+".xml", data=bin_data)
    

However, i'm getting:

    
    
    ...
    File "/home/hairo/sandbox/post_test.py", line 124, in post
    data = up.urlencode(('<?xml version="1.0" encoding="UTF-8"?>'
    ...
    raise TypeError
    TypeError: not a valid non-string sequence or mapping object
    

It looks like i'm missing something obvious, but i can't figure out what it
is, any help?


Answer: That call to
[urlencode](https://docs.python.org/2/library/urllib.html#urllib.urlencode) is
only passing a one-element tuple.
[Here](http://stackoverflow.com/questions/5607551/python-urlencode-string)'s
an example of what type of argument urlencode works with.

"Convert a mapping object or a sequence of two-element tuples".

