Jesus is Lord
Jesus is Lord

Reputation: 59

Using Python Requests library

Hey all you loved peoples, I have another one for you. I'm using django, requests, and google checkout. I am at the point of sending xml to Google checkout right. All is well EXCEPT. Using the requests library I am getting some content that I don't want in the POST. Let me explain. So google want s a correct XML file, got that, I'm using a sweet library to make Data structure from the schema. So my XML is correct. Requests though sends this to google.

--178.32.28.118.55290.2265475.1333156904.984.1
Content-Disposition: form-data; name="this.xml"; filename="../xml/this.xml"
Content-Type: application/xml

<?xml version="1.0" ?>
<checkout-shopping-cart xmlns='http://checkout.google.com/schema/2'>
<shopping-cart>
    <item>
        <digital-content>
            <url>/site_media/digitalGoods/Resume.html.pdf</url>
            <description>None Yet</description>
            <display-disposition>OPTIMISTIC</display-disposition>
        </digital-content>
        <item-name>Fire Safety Part 1</item-name>
        <item-description>&lt;p&gt;Pellentesque habitant morbi tristique senectus et   netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</item-description>
        <unit-price currency="USD">1.500000e+01</unit-price>
        <quantity>1</quantity>
        <merchant-item-id>1</merchant-item-id>
    </item>
</shopping-cart>
<checkout-flow-support>    
<merchant-checkout-flow-support/>
</checkout-flow-support>
</checkout-shopping-cart>
--178.32.28.118.55290.2265475.1333156904.984.1--

The problem I think is that requests is putting those numbers and those headers above the xml, like they are one document. Also it is writing those numbers directly after the xml. I think this is a problem because the error I get from my google integration console is.

 Error parsing XML; message from parser is: Content is not allowed in prolog.

SO my question is: Is there a way to turn this off, do i need to mangle the requests code my self, or what. Here is how I am POSTing with requets

#....other code and vars above
sendfile = {'this.xml':open('../xml/this.xml', 'r')}#the file
headers={'Authorization':("Basic %s" % auth),#google specific headers
        'Content-Type':'application/xml; charset=UTF-8',
        'Accept':'application/xml; charset=UTF-8'}
#send POST
r = requests.post(diagnose_turl, files=sendfile,headers=headers, verify=False)

Upvotes: 0

Views: 2537

Answers (1)

Paulo Scardine
Paulo Scardine

Reputation: 77241

The problem seems to be that you are trying to parse not only the XML, but the content-type header as well, and the parser is complaining as it is expecting a XML root element, not the "Content-Disposition" string.

This is strange, because if you do something like:

response = requests.post(some_url, ...)

The response.text is not supposed to include headers. If you are using the raw response, switch to response.text instead.

If you are getting the headers anyway, get rid of everything before the first blank line (\r\n\r\n) before feeding it to the parser:

import re
xml = '\n'.join(re.split(r'\r?\n\r?\n', raw_response)[1:])

Upvotes: 2

Related Questions