I'm performing test with POST and PUT method on a REST service.
Basically I use in both cases this xml:
xml = \
"""<?xml version="1.0" encoding="utf-8" ?>
<quser xmlns="http://user" xmlns:atom="http://www.w3.org/2005/atom">
<username>a_username</username>
<password>blablabla</password>
<first_name></first_name>
<last_name></last_name>
<email>anemail@gogle.com</email>
<degree></degree>
<institution></institution>
<fields>
<role>reader</role>
</fields>
<research_areas></research_areas>
</quser>"""
To POST this xml I use this code:
def post_resource(url, xml, content_type='application/xml'):
try:
request = urllib2.Request(url=url, data=xml)
request.add_header(key='content-type', val=content_type)
response = opener.open(request)
print_response(response)
except HTTPError, e:
print "Error: %s\n%s" % (e, e.read())
To perform changes (PUT) i use this code:
def put_resource(url, xml, username=None, password=None):
try:
if username and password:
str = "%s:%s" % (username, password)
b64 = str.encode('base64')
request = urllib2.Request(url=url, data=xml)
request.add_header(key='content-type', val='application/xml')
request.add_header(key='Authorization', val='Basic %s' % b64)
request.get_method = lambda: 'PUT'
response = opener.open(request)
print_response(response)
except HTTPError, e:
print "Error: %s\n%s" % (e, e.read())
Problem:
When I POST data everything goes fine. But when I tried to make changes on a resource, with PUT, I send the same xml with only a change in the email address, and the XML parser returns :
insert unclosed token: line 14, column 4
I've no idea how a similar xml can be cause of a parsing error in the PUT case and not in the POST case. Any suggestion is welcome! Thanks
EDIT
More details could help... I've access to the service implementation, and the parsing is made as follow:
from xml.etree.ElementTree import XML
try:
node_tree = XML(data.strip())
return self._parse_xml_node_to_dict(node_tree)
except ParseError, e:
When I debug, the exception it thrown when XML constructor is called with data.strip()
argument.
@thebjorn: I don't have any xml schema
@mzjn:
Thanks! So I've to track down where it happens, but the content of data
is truncated somewhere. Here is the content:
<?xml version="1.0" encoding="utf-8" ?>
<quser xmlns="http://user" xmlns:atom="http://www.w3.org/2005/atom">
<username>victorinox2</username>
<password>42b564oq</password>
<first_name></first_name>
<last_name></last_name>
<email>gosdfsdfsddacyer@gmail.com</email>
<degree></degree>
<institution></institution>
<fields>
<role>reader</role>
</fields>
<research_areas></research_areas>
</quse
I use Django and it seems that the request.raw_post_data contains already the truncated data. But when I ste data in urllib2.Request(url=url, data=xml)
the xml
content is not truncated...