Classic ASP and UPS API with XML

Slim81
Slim81 used Ask the Experts™
on
Hey guys and gals,
I am stuck.  XML is a bit confusing to me.... I have found the code posted below on a different site, but I can't seem to get it to work....  It just returns an empty select box.  I have x'd out my UPS details, for this post.

What am I missing here?
Is there any way to see where the error exists with the XML (ie telling me a line number)?

Here is the code:
<% Response.Buffer=True %>
<%
sAccessLicenseNumber = "xxxxxx"
sUserID = "xxxxxx"
sPassword = "xxxxxx"

DrawUPSRates "33", "50021"

Sub DrawUPSRates(sWeight, sDestinationPostalCode)

  sUPSXML = BuildUPSXML(sWeight, sDestinationPostalCode)

  'Now pass the request to UPS
  Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
  xmlhttp.Open "POST","https://wwwcie.ups.com/ups.app/xml/Rate",false
  xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlhttp.send sUPSXML

  sResponseXML = xmlhttp.responseText
  Set mydoc=Server.CreateObject("Microsoft.xmlDOM")
  mydoc.loadxml(sResponseXML)
  'Create a select table from the response xml
  response.Write("<select name='shipping'>")
  'Create A Nodelist of All The RatedShipments
  Set NodeList = mydoc.documentElement.selectNodes("RatedShipment")
  For x = 0 To NodeList.length - 1
    sDisplayString = _
    GetFriendlyUPSName(NodeList.Item(x).selectSingleNode("Service/Code").Text) & _
    " - $" & NodeList.Item(x).selectSingleNode("TotalCharges/MonetaryValue").Text
    Response.Write("<option>")
    Response.Write
    Response.Write("</option>")
  Next
  Response.Write("</select>")
End Sub

Function BuildUPSXML(sWeight, sDestinationPostalCode)
  sShipperPostalCode = "63126"
 
  sXML = sXML & "<?xml version='1.0'?>"
  sXML = sXML & "    <AccessRequest xml:lang='en-US'>"
  sXML = sXML & "        <AccessLicenseNumber>" & sAccessLiscenseNumber & _
                "</AccessLicenseNumber>"
  sXML = sXML & "        <UserId>" & sUserID & "</UserId>"
  sXML = sXML & "        <Password>" & sPassword & "</Password>"
  sXML = sXML & "    </AccessRequest>"
  sXML = sXML & "<?xml version='1.0'?>"
  sXML = sXML & "    <RatingServiceSelectionRequest xml:lang='en-US'>"
  sXML = sXML & "        <Request>"
  sXML = sXML & "            <TransactionReference>"
  sXML = sXML & "                <CustomerContext>Rating and " & _
                "Service</CustomerContext>"
  sXML = sXML & "                <XpciVersion>1.0001</XpciVersion>"
  sXML = sXML & "            </TransactionReference>"
  sXML = sXML & "            <RequestAction>Rate</RequestAction>"
  sXML = sXML & "            <RequestOption>shop</RequestOption>"
  sXML = sXML & "        </Request>"
  sXML = sXML & "        <PickupType>"
  sXML = sXML & "            <Code>01</Code>"
  sXML = sXML & "        </PickupType>"
  sXML = sXML & "        <Shipment>"
  sXML = sXML & "            <Shipper>"
  sXML = sXML & "                <Address>"
  sXML = sXML & "                    <PostalCode>" & sShipperPostalCode & _
                "</PostalCode>"
  sXML = sXML & "                </Address>"
  sXML = sXML & "            </Shipper>"
  sXML = sXML & "            <ShipTo>"
  sXML = sXML & "                <Address>"
  sXML = sXML & "                    <PostalCode>" & sDestinationPostalCode & _
                "</PostalCode>"
  sXML = sXML & "                </Address>"
  sXML = sXML & "            </ShipTo>"
  sXML = sXML & "            <Service>"
  sXML = sXML & "                <Code>11</Code>"
  sXML = sXML & "            </Service>"
  sXML = sXML & "            <Package>"
  sXML = sXML & "                <PackagingType>"
  sXML = sXML & "                    <Code>02</Code>"
  sXML = sXML & "                    <Description>Package</Description>"
  sXML = sXML & "                </PackagingType>"
  sXML = sXML & "                <Description>Rate Shopping</Description>"
  sXML = sXML & "                <PackageWeight>"
  sXML = sXML & "                    <Weight>" & sWeight & "</Weight>"
  sXML = sXML & "                </PackageWeight>"
  sXML = sXML & "            </Package>"
  sXML = sXML & "            <ShipmentServiceOptions/>"
  sXML = sXML & "        </Shipment>"
  sXML = sXML & "</RatingServiceSelectionRequest>"
 
  BuildUPSXML = Replace(sXML, vbTab, "")
End Function

Function GetFriendlyUPSName(vCode)
  Select Case vCode
    Case "01"
      GetFriendlyUPSName = "UPS Next Day Air"
    Case "02"
      GetFriendlyUPSName = "UPS 2nd Day Air"
    Case "03"
      GetFriendlyUPSName = "UPS Ground"
    Case "07"
      GetFriendlyUPSName = "UPS Worldwide Express"
    Case "08"
      GetFriendlyUPSName = "UPS Worldwide Expedited"
    Case "11"
      GetFriendlyUPSName = "UPS Standard"
    Case "12"
      GetFriendlyUPSName = "UPS 3 Day Select"
    Case "13"
      GetFriendlyUPSName = "UPS Next Day Air Saver"
    Case "14"
      GetFriendlyUPSName = "UPS Next Day Air Early A.M."
    Case "54"
      GetFriendlyUPSName = "UPS Worldwide Express Plus"
    Case "59"
      GetFriendlyUPSName = "UPS 2nd Day Air A.M."
    Case "65"
      GetFriendlyUPSName = "UPS Saver"
  End Select
End Function

%>

Open in new window


Any help would be appreciated!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
Commented:
Looks like the Access Number is not supplied properly, correct the variable name passed in the XML document that is created.

LINE 3: sAccessLicenseNumber = "xxxxxx"

LINE 42:   sXML = sXML & "        <AccessLicenseNumber>" & sAccessLiscenseNumber & _

Also for debugging you can print value as Response.Write(xmlhttp.responseText) to see what was the return value.

Author

Commented:
pateljitu,
Thanks for the response.  I mentioned in my first post that "I have x'd out my UPS details, for this post", this way I wasn't sharing my information with teh EE world.

I will try the resonse.write(xmlhttp.responseText) and report back.

Thanks!
Top Expert 2011

Commented:
Correction: I actually did not mean the value of the variable, the variable name itself is inconsistent.

at line 3: the variable name is called sAccessLicenseNumber, but when you are assign the variable in XML node at line 42: <AccessLicenseNumber>" & sAccessLiscenseNumber & _
                "</AccessLicenseNumber>" the variable name is not the same as on line number 3, there is extra character "s" in the variable name.


So when the XML is submitted the value supplied is BLANK or NULL instead of legit value.

Author

Commented:
pateljitu,
I stand corrected!  You were right, AccessLiscenseNumber <> AccessLicenseNumber....

The response.write(xmlhttp.responseText) pointed me right to that problem.

There was an issue with the way the select field was populated too, but I was able to fix that.

Thanks for your help!

For all future EE searchers that want an ASP UPS XML solution here it is:
<% Response.Buffer=True %>
<%
sAccessLicenseNumber = "xxxxxx"
sUserID = "xxxxxx"
sPassword = "xxxxxx"

DrawUPSRates "33", "50021"

Sub DrawUPSRates(sWeight, sDestinationPostalCode)

  sUPSXML = BuildUPSXML(sWeight, sDestinationPostalCode)

  'Now pass the request to UPS
  Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
  xmlhttp.Open "POST","https://wwwcie.ups.com/ups.app/xml/Rate",false
  xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlhttp.send sUPSXML

  sResponseXML = xmlhttp.responseText
  Set mydoc=Server.CreateObject("Microsoft.xmlDOM")
  mydoc.loadxml(sResponseXML)
  'Create a select table from the response xml
  response.Write("<select name='shipping'>")
  'Create A Nodelist of All The RatedShipments
  Set NodeList = mydoc.documentElement.selectNodes("RatedShipment")
  For x = 0 To NodeList.length - 1
    sDisplayString = _
    GetFriendlyUPSName(NodeList.Item(x).selectSingleNode("Service/Code").Text) & _
    " - $" & NodeList.Item(x).selectSingleNode("TotalCharges/MonetaryValue").Text
    Response.Write("<option>")
    Response.Write (sDisplayString)
    Response.Write("</option>")
  Next
  Response.Write("</select>")
  'response.write(xmlhttp.responseText)
End Sub

Function BuildUPSXML(sWeight, sDestinationPostalCode)
  sShipperPostalCode = "63126"
 
  sXML = sXML & "<?xml version='1.0'?>"
  sXML = sXML & "    <AccessRequest xml:lang='en-US'>"
  sXML = sXML & "        <AccessLicenseNumber>" & sAccessLicenseNumber & _
                "</AccessLicenseNumber>"
  sXML = sXML & "        <UserId>" & sUserID & "</UserId>"
  sXML = sXML & "        <Password>" & sPassword & "</Password>"
  sXML = sXML & "    </AccessRequest>"
  sXML = sXML & "<?xml version='1.0'?>"
  sXML = sXML & "    <RatingServiceSelectionRequest xml:lang='en-US'>"
  sXML = sXML & "        <Request>"
  sXML = sXML & "            <TransactionReference>"
  sXML = sXML & "                <CustomerContext>Rating and " & _
                "Service</CustomerContext>"
  sXML = sXML & "                <XpciVersion>1.0001</XpciVersion>"
  sXML = sXML & "            </TransactionReference>"
  sXML = sXML & "            <RequestAction>Rate</RequestAction>"
  sXML = sXML & "            <RequestOption>shop</RequestOption>"
  sXML = sXML & "        </Request>"
  sXML = sXML & "        <PickupType>"
  sXML = sXML & "            <Code>01</Code>"
  sXML = sXML & "        </PickupType>"
  sXML = sXML & "        <Shipment>"
  sXML = sXML & "            <Shipper>"
  sXML = sXML & "                <Address>"
  sXML = sXML & "                    <PostalCode>" & sShipperPostalCode & _
                "</PostalCode>"
  sXML = sXML & "                </Address>"
  sXML = sXML & "            </Shipper>"
  sXML = sXML & "            <ShipTo>"
  sXML = sXML & "                <Address>"
  sXML = sXML & "                    <PostalCode>" & sDestinationPostalCode & _
                "</PostalCode>"
  sXML = sXML & "                </Address>"
  sXML = sXML & "            </ShipTo>"
  sXML = sXML & "            <Service>"
  sXML = sXML & "                <Code>11</Code>"
  sXML = sXML & "            </Service>"
  sXML = sXML & "            <Package>"
  sXML = sXML & "                <PackagingType>"
  sXML = sXML & "                    <Code>02</Code>"
  sXML = sXML & "                    <Description>Package</Description>"
  sXML = sXML & "                </PackagingType>"
  sXML = sXML & "                <Description>Rate Shopping</Description>"
  sXML = sXML & "                <PackageWeight>"
  sXML = sXML & "                    <Weight>" & sWeight & "</Weight>"
  sXML = sXML & "                </PackageWeight>"
  sXML = sXML & "            </Package>"
  sXML = sXML & "            <ShipmentServiceOptions/>"
  sXML = sXML & "        </Shipment>"
  sXML = sXML & "</RatingServiceSelectionRequest>"
 
  BuildUPSXML = Replace(sXML, vbTab, "")
End Function

Function GetFriendlyUPSName(vCode)
  Select Case vCode
    Case "01"
      GetFriendlyUPSName = "UPS Next Day Air"
    Case "02"
      GetFriendlyUPSName = "UPS 2nd Day Air"
    Case "03"
      GetFriendlyUPSName = "UPS Ground"
    Case "07"
      GetFriendlyUPSName = "UPS Worldwide Express"
    Case "08"
      GetFriendlyUPSName = "UPS Worldwide Expedited"
    Case "11"
      GetFriendlyUPSName = "UPS Standard"
    Case "12"
      GetFriendlyUPSName = "UPS 3 Day Select"
    Case "13"
      GetFriendlyUPSName = "UPS Next Day Air Saver"
    Case "14"
      GetFriendlyUPSName = "UPS Next Day Air Early A.M."
    Case "54"
      GetFriendlyUPSName = "UPS Worldwide Express Plus"
    Case "59"
      GetFriendlyUPSName = "UPS 2nd Day Air A.M."
    Case "65"
      GetFriendlyUPSName = "UPS Saver"
  End Select
End Function

%>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial