0

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>[email protected]</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>[email protected]</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...

2
  • You may want to try the Requests package instead of urllib2 for your HTTP PUT, as mentioned here: stackoverflow.com/questions/111945/…
    – Joe Bane
    May 5, 2012 at 12:45
  • 1
    Can you show us the contents of the data variable?
    – mzjn
    May 5, 2012 at 14:00

2 Answers 2

1

If you have the schema of the service you're using, you could always try to validate the xml yourself... might give you a more informative error message..

1

So apparently, as I said in my previous EDIT, the string transmitted is truncated when it arrives on the service side. I couldn't find out if it was Django-related or urllib2-related.

Anyway, I figured this out by adding two new lines at the end of the string contained in the xml varibale. One new line wasn't enough there was still the > truncated.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.