Click here to Skip to main content
14,971,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm newer in python. I'm trying to send to send a xml file using HTTP POST request with a body and i received this error:
b'<?xml version="1.0" encoding="utf-8"?>\r\n<ResponseDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r\n  <response>\r\n    <index>1</index>\r\n    <statusCode>XMLSyntaxError</statusCode>\r\n    <errors>\r\n      <error>\r\n        <message>Data at the root level is invalid. Line 1, position 1.</message>\r\n        <code>101</code>\r\n      </error>\r\n    </errors>\r\n  </response>\r\n</ResponseDoc>'


I used Python 3.7.
Here is my code:


What I have tried:

import http.client
import base64
import urllib.request
import urllib.response
import urllib.parse
import urllib.error
    file = 'sendInvoices.xml'
    headers = {
        # Request headers
        'aade-user-id': 'stergos73',
        'Ocp-Apim-Subscription-Key': 'c3d8fbf2eb984942a20f9b181139fc4c',
    }
    
    params = urllib.parse.urlencode({
    })
    
    try:
        conn = http.client.HTTPSConnection('mydata-dev.azure-api.net')
        conn.request("POST", "/SendIncomeClassification?%s" % params, file, headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))`enter code here`


My xml file:

<?xml version="1.0" encoding="UTF-8"?>
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.aade.gr/myDATA/invoice/v1.0/InvoicesDoc-v0.6.xsd" xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0">
            <invoice>
                <issuer>
                    <vatNumber>1xxxxxxxxx</vatNumber>
                    <country>GR</country>
                    <branch>1</branch>                
                </issuer>
                <counterpart>
                    <vatNumber>0xxxxxxxxx</vatNumber>
                    <country>GR</country>
                    <branch>0</branch>            
                    <address>                
                        <postalCode>22222</postalCode>
                        <city>IRAKLIO</city>
                    </address>
                </counterpart>
                <invoiceHeader>
                    <series>A</series>
                    <aa>101</aa>
                    <issueDate>2020-04-08</issueDate>
                    <invoiceType>5.1</invoiceType>
                    <currency>EUR</currency>
                    <correlatedInvoices>400000020342040</correlatedInvoices>
                </invoiceHeader>
                <paymentMethods>
                    <paymentMethodDetails>
                        <type>3</type>
                        <amount>1760.00</amount>
                        <paymentMethodInfo>Payment Method Info...</paymentMethodInfo>
                    </paymentMethodDetails>
                </paymentMethods>
                <invoiceDetails>
                    <lineNumber>1</lineNumber>
                    <netValue>1000.00</netValue>
                    <vatCategory>1</vatCategory>
                    <vatAmount>240.00</vatAmount>           
                    <incomeClassification>
                        <icls:classificationType>E3_561_001</icls:classificationType>
                        <icls:classificationCategory>category1_2</icls:classificationCategory>
                        <icls:amount>1000.00</icls:amount>
                    </incomeClassification>
                </invoiceDetails>
                <invoiceDetails>            
                    <lineNumber>2</lineNumber>     
                    <netValue>500.00</netValue>
                    <vatCategory>1</vatCategory>
                    <vatAmount>120.00</vatAmount>
                    <discountOption>true</discountOption>           
                    <incomeClassification>
                        <icls:classificationType>E3_561_001</icls:classificationType>
                        <icls:classificationCategory>category1_3</icls:classificationCategory>
                        <icls:amount>500.00</icls:amount>
                    </incomeClassification>
                </invoiceDetails>       
                <invoiceSummary>
                    <totalNetValue>1500.00</totalNetValue>
                    <totalVatAmount>360.00</totalVatAmount>
                    <totalWithheldAmount>00.00</totalWithheldAmount>
                    <totalFeesAmount>0.00</totalFeesAmount>
                    <totalStampDutyAmount>0.00</totalStampDutyAmount>
                    <totalOtherTaxesAmount>0.00</totalOtherTaxesAmount>
                    <totalDeductionsAmount>0.00</totalDeductionsAmount>
                    <totalGrossValue>1860.00</totalGrossValue>
                    <incomeClassification>
                        <icls:classificationType>E3_561_001</icls:classificationType>
                        <icls:classificationCategory>category1_2</icls:classificationCategory>
                        <icls:amount>1000.00</icls:amount>              
                    </incomeClassification>
                        <incomeClassification>
                        <icls:classificationType>E3_561_001</icls:classificationType>
                        <icls:classificationCategory>category1_3</icls:classificationCategory>
                        <icls:amount>500.00</icls:amount>               
                    </incomeClassification>
                </invoiceSummary>
            </invoice>
        </InvoicesDoc>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900