1

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?

2
  • Which line is associated with the error? Commented Dec 16, 2014 at 3:23
  • "Convert a mapping object or a sequence of two-element tuples". Your argument is a one-element tuple. Commented Dec 16, 2014 at 3:30

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i solved it with up.urlencode({'data': ('<?xml version="1.0" encoding="UTF-8"?>'...)})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.