Does anyone have an example on how to use the ups xml api for rating package shipments?

4.2k views Asked by At

I saw a few examples when using php or python 2.7 but not python 3.2. Thanks in advance!

1

There are 1 answers

0
Andrew Kloos On BEST ANSWER

Came up with this...

import urllib.request, urllib.error

data ="""
<?xml version="1.0" ?>
<AccessRequest xml:lang="en-US">
    <AccessLicenseNumber>XXXXX</AccessLicenseNumber>
    <UserId>XXXX</UserId>
    <Password>XXXXX</Password>
</AccessRequest>
<?xml version="1.0" ?>
<RatingServiceSelectionRequest xml:lang="en-US">
    <Request>
        <TransactionReference>
            <CustomerContext>Bare Bones Rate Request</CustomerContext>
            <XpciVersion>1.0</XpciVersion>
        </TransactionReference>
        <RequestAction>Rate</RequestAction>
        <RequestOption>Rate</RequestOption>
    </Request>
    <PickupType>
        <Code>01</Code>
    </PickupType>
    <Shipment>
        <Shipper>
            <Address>
                <PostalCode>44111</PostalCode>
        <StateProvinceCode>OH</StateProvinceCode>
                <CountryCode>US</CountryCode>
            </Address>
        </Shipper>
        <ShipTo>
            <Address>
                <PostalCode>74403</PostalCode>
        <StateProvinceCode>OK</StateProvinceCode>
                <CountryCode>US</CountryCode>
            <ResidentialAddressIndicator/>
            </Address>
        </ShipTo>
        <ShipFrom>
            <Address>
                <PostalCode>44023</PostalCode>
        <StateProvinceCode>OH</StateProvinceCode>
                <City>Chagrin Falls</City>
                <CountryCode>US</CountryCode>
            </Address>
        </ShipFrom>
        <Service>
            <Code>03</Code>
        </Service>
        <Package>
            <PackagingType>
                <Code>02</Code>
            </PackagingType>
            <Dimensions>
                <UnitOfMeasurement>
                <Code>IN</Code>
                </UnitOfMeasurement>
                <Length>8.0</Length>
                <Width>8.0</Width>
                <Height>8.0</Height>
            </Dimensions>
            <PackageWeight>
                <UnitOfMeasurement>
                <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>12.0</Weight>
            </PackageWeight>
        </Package>
    </Shipment>
</RatingServiceSelectionRequest>
"""

try:
    httpresq = urllib.request.Request(url="https://wwwcie.ups.com/ups.app/xml/Rate", data=data.encode('utf_8'), headers={'Content-Type': 'application/x-www-form-urlencoded'})
    response = urllib.request.urlopen(httpresq)
    return_values = response.read()
    print(return_values)
except urllib.error.URLError as e:
    error = "urllib.error.URLError exception was raised: %s" % e
    print(error)