使用Python脚本进行Mozenda批量插入

使用Python脚本进行Mozenda批量插入,python,api,bulk,insertion,Python,Api,Bulk,Insertion,我正试图编写一个python脚本,在MozendaAPI上执行批量插入。他们在文档中用C#给出了一个例子 xml-下面的示例 <ItemList> <Item> <First>32</First> <Second>03</Second> <Third>403</Third> <Fourth>015</Fourth> <Fifth

我正试图编写一个python脚本,在MozendaAPI上执行批量插入。他们在文档中用C#给出了一个例子

xml-下面的示例

<ItemList>
  <Item>
    <First>32</First>
    <Second>03</Second>
    <Third>403</Third>
    <Fourth>015</Fourth>
    <Fifth>0000</Fifth>
    <PIN>32034030150000</PIN>
  </Item>
</ItemList>
输出:

<?xml version="1.0" encoding="utf-8"?>
<CollectionAddItemResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Result>Success</Result>
  <ItemID>1056</ItemID>
</CollectionAddItemResponse>

您没有正确设置请求。在执行POST请求时,必须按照

下面是一个Python示例

from urllib2 import urlopen, Request
from string import ascii_uppercase, digits
from random import choice

# Open your data file
with open("C:\datafile.xml", "r") as data_file:
    data_file_string = data_file.read()

# The URL
url = 'https://api.mozenda.com/rest?WebServiceKey=[CANNOT-PROVIDE]&Service=Mozenda10&Operation=Collection.AddItem&CollectionID=1037'

# The boundary delimits where the file to be uploaded starts and where it ends.
boundary = "".join(choice(ascii_uppercase + digits) 
                   for x in range(20))
body_list = []
body_list.append("--" + boundary)
body_list.append("Content-Disposition: form-data;"
                 " name='file'; filename=''")
body_list.append("Content-Type: application/octet-stream")
body_list.append("")
body_list.append(data_file_string)
body_list.append("--{0}--".format(boundary))
body_list.append("")
body="\r\n".join(body_list).encode("utf8")

content_type = ("multipart/form-data; boundary="
                "{0}").format(boundary)
headers = {"Content-Type": content_type,
           "Content-Length": str(len(body))} # Tells how big the content is

request = Request(url, body, headers)

result = urlopen(url=request).read()

我将fileName='/Users/me/Desktop/test.xml'修复为fileName='\Users\me/Desktop\test.xml',但运气不佳。这肯定是问题所在。谢谢你的反馈。当我运行脚本时,我得到以下信息:File“/home/me/PycharmProjects/chicago data/mozenda.py”,第26行,在body=“\r\n”.join(body_list).encode(“utf8”)TypeError:sequence item 4:预期的字符串,File found您需要确保将文件的内容读入字符串。非常感谢您的帮助!!!我更改了body_list.append(数据文件)到body_list.append(数据文件字符串)后,POST将按预期工作!不客气。我修改了代码以备将来参考。
string webServiceKey = C70E1F84-E12B-4e73-B199-2EE6D43AF44E; //Your Account WebServiceKey.
string collectionID = 1001; //The ID of the destination Collection.
string url = string.Format(
"https://api.mozenda.com/rest?WebServiceKey={0}&Service=Mozenda10&Operation=Collection.AddItem&CollectionID={1}",
webServiceKey, collectionID);
string fileName = "C:\\Temp\\NewItems.xml"; //Path to the file containing the items to be uploaded.

WebClient client = new WebClient();
byte[] responseBinary = client.UploadFile(url, fileName);
string response = Encoding.UTF8.GetString(responseBinary);
from urllib2 import urlopen, Request
from string import ascii_uppercase, digits
from random import choice

# Open your data file
with open("C:\datafile.xml", "r") as data_file:
    data_file_string = data_file.read()

# The URL
url = 'https://api.mozenda.com/rest?WebServiceKey=[CANNOT-PROVIDE]&Service=Mozenda10&Operation=Collection.AddItem&CollectionID=1037'

# The boundary delimits where the file to be uploaded starts and where it ends.
boundary = "".join(choice(ascii_uppercase + digits) 
                   for x in range(20))
body_list = []
body_list.append("--" + boundary)
body_list.append("Content-Disposition: form-data;"
                 " name='file'; filename=''")
body_list.append("Content-Type: application/octet-stream")
body_list.append("")
body_list.append(data_file_string)
body_list.append("--{0}--".format(boundary))
body_list.append("")
body="\r\n".join(body_list).encode("utf8")

content_type = ("multipart/form-data; boundary="
                "{0}").format(boundary)
headers = {"Content-Type": content_type,
           "Content-Length": str(len(body))} # Tells how big the content is

request = Request(url, body, headers)

result = urlopen(url=request).read()