0

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)
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Jesus is Lord
  • 59
  • 1
  • 7

1 Answers1

2

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:])
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • Right I understand that, thank you for the response. I am asking if there is a way to edit the request to not do that. – Jesus is Lord Mar 31 '12 at 18:02
  • Thanks for the response, I still think it is something with the Requests module, because the xml on the server(static file) doesn't have anything like that there. A get wouldn't work I don't think because I nee to POST the file to google. – Jesus is Lord Mar 31 '12 at 18:30
  • Hey I solved the problem using httplib, if you want i'll give you how I did it so you can answer the question with it because I don't have the rep to answer my own. Any ways thanks man, and Jesus really loves you. Okay, have a great day Paulo – Jesus is Lord Mar 31 '12 at 23:06
  • @JesusisLord: post or get, `r.text` is not supposed to include any headers, so be sure to access the xml through `r.text` and not `r.raw` – Paulo Scardine Apr 01 '12 at 11:35