0

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?

Hairo
  • 2,062
  • 5
  • 27
  • 33

1 Answers1

1

That call to urlencode is only passing a one-element tuple. Here's an example of what type of argument urlencode works with.

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

Community
  • 1
  • 1
Wyrmwood
  • 3,340
  • 29
  • 33