使用POST请求发送XML

2024-06-26 14:01:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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'\r\nxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r\n \r\n >1\r\n XMLSyntaxE ```

我使用了Python 3.7。
这是我的密码:

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`

这里是我的xml文件:

<?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>

Tags: importhttpwwwxmlurllibconnamountgr