使用post请求发送xml数据

时间:2014-02-04 09:49:26

标签: python xml post

http://api.shipstation.com/Order-Resource.ashx

以上网址是shipstation API文档,用于提取订单,创建订单,更新和删除。

我想使用创建订单API,即POST请求,其中订单数据条目使用xml发送到shipstation。

我的问题是如何使用Python使用POST请求将xml数据发送到shipstation?

由于我无法在此页面上发布完整代码,因此请参阅此网址以查看创建订单的帖子请求 -

http://api.shipstation.com/Order-Resource.ashx

由于

2 个答案:

答案 0 :(得分:2)

你应该这样试试

def frame_xml(AddressVerified,AmountPaid):
    xml_data = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/ metadata" xmlns="http://www.w3.org/2005/Atom">
    <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="SS.WebData.Order" />
     <title />
     <author>
     <name />
     </author>
     <id />
     <content type="application/xml">
     <m:properties>
       <d:AddressVerified m:type="Edm.Byte">%s</d:AddressVerified>
       <d:AmountPaid m:type="Edm.Decimal">%s</d:AmountPaid>
     </m:properties>
    </content>
   </entry>"""%(AddressVerified,AmountPaid)
   return xml_data

headers = {'Content-Type': 'application/xml'}
xml_data = frame_xml('AddressVerified','AmountPaid')
print requests.post('https://data.shipstation.com/1.2/Orders', data=xml_data, headers=headers).text

答案 1 :(得分:0)

使用Python requests模块。您可以在快速入门页面上找到一些示例:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print r.text
    ...

或者

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
    ...

在此处查看更多内容:How can I send an xml body using requests library?