Tuesday, December 11, 2018

The good-enough-for-government-work USPS API offers SOAP messaging without a WSDL.

The United States Postal Service has an API for trying to suggest a better street address for a given address in the name of trying to not screw up orders in the mail. You have to register here and then you will be given a username and a password. I do not know what you might do with the password, but you may use the username like so:

https://secure.shippingapis.com/ShippingAPI.dll?API=Verify&XML=
<AddressValidateRequest USERID="LETMEIN">
   <Revision>1</Revision>
   <Address ID="0">
      <Address1>8080 Eden Road</Address1>
      <Address2></Address2>
      <City>Eden Prairie</City>
      <State>MN</State>
      <Zip5>55344</Zip5>
      <Zip4></Zip4>
   </Address>
</AddressValidateRequest>

 
 

What is above may just be copied and pasted into the URL line of a browser to make this stuff work. (Well, LETMEIN needs to be swapped out with a better username.) You make GET calls and instead of passing URL line variables in the usual way you pass a bunch of XML. I am serious. This and this has some more about it. What is above returns the following because I left off my apartment number.

<?xml version="1.0" encoding="ISO-8859-1"?>
<AddressValidateResponse>
   <Address ID="0">
      <Address2>8080 EDEN RD</Address2>
      <City>EDEN PRAIRIE</City>
      <State>MN</State>
      <Zip5>55344</Zip5>
      <Zip4>5309</Zip4>
      <DeliveryPoint>99</DeliveryPoint>
      <ReturnText>Default address: The address you entered was found but more
            information is needed (such as an apartment, suite, or box number) to match to a
            specific address.</ReturnText>
      <CarrierRoute>C002</CarrierRoute>
      <Footnotes>HN</Footnotes>
      <DPVConfirmation>D</DPVConfirmation>
      <DPVCMRA>N</DPVCMRA>
      <DPVFootnotes>AAN1</DPVFootnotes>
      <Business>N</Business>
      <CentralDeliveryPoint>N</CentralDeliveryPoint>
      <Vacant>N</Vacant>
   </Address>
</AddressValidateResponse>

 
 

I think you may lead in with http://production.shippingapis.com/ instead of https://secure.shippingapis.com/ but I don't know why you would. If I put in my apartment number like so...

https://secure.shippingapis.com/ShippingAPI.dll?API=Verify&XML=
<AddressValidateRequest USERID="LETMEIN">
   <Revision>1</Revision>
   <Address ID="0">
      <Address1>8080 Eden Road</Address1>
      <Address2>261</Address2>
      <City>Eden Prairie</City>
      <State>MN</State>
      <Zip5>55344</Zip5>
      <Zip4></Zip4>
   </Address>
</AddressValidateRequest>

 
 

...I get this instead:

<?xml version="1.0" encoding="ISO-8859-1"?>
<AddressValidateResponse>
   <Address ID="0">
      <Address1>APT 261</Address1>
      <Address2>8080 EDEN RD</Address2>
      <City>EDEN PRAIRIE</City>
      <State>MN</State>
      <Zip5>55344</Zip5>
      <Zip4>7604</Zip4>
      <DeliveryPoint>61</DeliveryPoint>
      <CarrierRoute>C002</CarrierRoute>
      <Footnotes>N</Footnotes>
      <DPVConfirmation>Y</DPVConfirmation>
      <DPVCMRA>N</DPVCMRA>
      <DPVFootnotes>AABB</DPVFootnotes>
      <Business>N</Business>
      <CentralDeliveryPoint>Y</CentralDeliveryPoint>
      <Vacant>N</Vacant>
   </Address>
</AddressValidateResponse>

 
 

When I make the very human mistake of leading my apartment number with a pound sign...

https://secure.shippingapis.com/ShippingAPI.dll?API=Verify&XML=
<AddressValidateRequest USERID="LETMEIN">
   <Revision>1</Revision>
   <Address ID="0">
      <Address1>8080 Eden Road</Address1>
      <Address2>#261</Address2>
      <City>Eden Prairie</City>
      <State>MN</State>
      <Zip5>55344</Zip5>
      <Zip4></Zip4>
   </Address>
</AddressValidateRequest>

 
 

Look at this!

<?xml version="1.0" encoding="ISO-8859-1"?>
<Error>
   <Number>80040B19</Number>
   <Description>XML Syntax Error: Please check the XML request to see if it can be
         parsed.(B)</Description>
   <Source>USPSCOM::DoAuth</Source>
</Error>

No comments:

Post a Comment