2Sep/09
Calling the USPS API via Classic ASP
For years I have been thinking about writing an asp interface for the UPS and USPS rate finder API. But since I had a com component installed on the one server I used it on, I always put it off and used the component. As luck would have it, one Sunday a few weeks ago (it's always a Sunday) the component decided to return errors from UPS. Of course, the component writer is long gone, so it was time to write the interface I had been contemplating for years. Now mind you, this was on a very busy ecommerce site, so I was in a rush.
Surprisingly, I had some asp code up and running quickly. Here is the simple code in ASP that calls the USPS API and reads the results. This can be plugged in easily into any code that needs the USPS rate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <% 'needs the following variables weight=1 ship_to_zip=33323 package_width=6 package_length=6 'package_height=6 if int(weight) <> weight then weight=int(weight) + 1 end if if package_width < 6 then package_width=6 if package_length < 6 then package_length=6 if package_height < 6 then package_height=6 xml_request= "<?xml version='1.0'?>" xml_request = xml_request & "<rateV3Request USERID='YOUR_USERID'>" if usps_service="" then usps_service="PRIORITY" end if xml_request = xml_request & "<package ID='1ST'>" xml_request = xml_request & "<service>" & usps_service & "</service>" xml_request = xml_request & "<zipOrigination>33323</zipOrigination>" xml_request = xml_request & "<zipDestination>" & ship_to_zip & "</zipDestination>" xml_request = xml_request & "<pounds>" & weight & "</pounds>" xml_request = xml_request & "<ounces>0</ounces>" xml_request = xml_request & "<container></container>" xml_request = xml_request & "<size>REGULAR</size>" xml_request = xml_request & "<width>" & package_width & "</width>" xml_request = xml_request & "<length>" & package_length & "</length>" xml_request = xml_request & "<height>" & package_height & "</height>" 'xml_request = xml_request & "<girth>55</girth>" if usps_service="PARCEL POST" then xml_request = xml_request & "<machinable>true</machinable>" end if xml_request = xml_request & "</package>" xml_request = xml_request & "</rateV3Request>" URLToRSS = "http://Production.ShippingAPIs.com/ShippingAPI.dll" Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0") xmlHttp.Open "Post", URLToRSS, false xmlHttp.Send("API=RateV3&XML=" & xml_request) 'uncomment below to see what USPS API is returning 'Response.write xmlHttp.ResponseTEXT Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.3.0") xmlDOM.async = false xmlDOM.setProperty "SelectionLanguage", "XPath" xmlDOM.LoadXML xmlHttp.ResponseTEXT if xmlDOM.parseError.errorCode <> 0 then response.write "DOM Not Loaded" end if xmlQuery = "/RateV3Response/Package/Postage" set docHeadlines = xmlDOM.documentElement.selectNodes(xmlQuery) rate_error="" total=0 total= docHeadlines.item(0).selectSingleNode("Rate").nodeTypedValue 'returns total with rate %> |