关于python:带有请求的POST XML文件

POST XML file with requests

我越来越:

1
<error>You have an error in your XML syntax...

当我运行此python脚本时,我刚刚写过(我是新手)

1
2
3
4
5
6
7
8
9
import requests

xml ="""xxx.xml"""

headers = {'Content-Type':'text/xml'}

r = requests.post('https://example.com/serverxml.asp', data=xml)

print (r.content);

这是xxx.xml的内容

1
2
3
4
5
6
7
8
<xml>
<API>4.0</API>
login</action>
<password>xxxx</password>
<license_number>xxxxx</license_number>
<username>xxx@xyz.com</username>
<training>1</training>
</xml>

我知道xml是有效的,因为我为perl脚本使用了相同的xml,并且内容被打印回去。

任何帮助将不胜感激,因为我是python的新手。


您想要将XML数据从文件提供给requests.post。 但是,此功能不会为您打开文件。 它希望您将文件对象而不是文件名传递给它。 您需要在调用request.post之前打开文件。

尝试这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
import requests

# Set the name of the XML file.
xml_file ="xxx.xml"

headers = {'Content-Type':'text/xml'}

# Open the XML file.
with open(xml_file) as xml:
    # Give the object representing the XML file to requests.post.
    r = requests.post('https://example.com/serverxml.asp', data=xml)

print (r.content);