2017-02-01 31 views
0

我正在使用USPS API跟踪包想要对卷包进行跟踪的请求。如何使USPS的卷曲请求django python

这是我在USPS API文档中找到的跟踪包的内容。

> http://production.shippingapis.com/ShippingApi.dll?API=TrackV2&XML=<TrackFieldRequest 
> USERID="xxxxxxxxxx"> <TrackID ID="XXXXXXXXXXXXX"> </TrackID> 
> 
> </TrackFieldRequest> 

现在我试图让在Django卷曲的要求,这是我在做什么,但它没有这个working.Is解析XML/URL在Django正确的方式。

def get_tracking_status(self): 
     try: 
      headers = {'Content-Type': 'application/xml'} 
      xml = "<TrackFieldRequest USERID='xxxxxxxxxx'><TrackID ID='XXXXXXXXXXXXX'></TrackID></TrackFieldRequest>" 
      requests.post("http://production.shippingapis.com/ShippingApi.dll?API=TrackV2", headers=headers, data=xml) 
     except Exception as e: 
      print e 

回答

0

您通过卷曲使该请求有两个查询参数:API,其值为“TrackV2”,和XML,它的值是XML二进制大对象。你应该做同样的要求:也

requests.get("http://production.shippingapis.com/ShippingApi.dll", data={'API': 'TrackV2', 'XML': xml}, headers=headers) 

注意这似乎是一个GET,不是POST。

+0

感谢兄弟你的解决方案。 –