Search code examples
pythondjangocurlusps

How to make Curl request for USPS django python


I am working on USPS API for tracking packages i want to make curl request for tracking package.

This is what i found on USPS API documentation for tracking package.

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

Now i am trying to make curl request in django this is what i am doing but it does not working.Is this a right way to parse xml/url in 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

Solution

  • The request you make via CURL has two query parameters: API, whose value is "TrackV2", and XML, whose value is the xml blob. You should just do the same with requests:

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

    Note also it appears to be a GET, not a POST.