function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce-chrissalesforce-chris 

USPS - error response from test servers

I am receiving an error message from your test servers where the error code is 2147219093 and the description is in regards to an invalid character.  However, even when I hard-code the HTTP request string (rather than build it dynamically) with the information and send it I am receiving an error code from the USPS test servers.  More interesting, when I copy the endpoint text from the logs in Salesforce I am able to get a response from the test servers when making the request manually using a browser.  The relevant portion of my Salesforce log is listed below.  I get a status code of 200 but it is then followed by an error bad character reference?  Has anyone worked with the USPS web tools that can offer suggestions?

 

10:09:42.083 (83092000)|SYSTEM_METHOD_ENTRY|[38]|System.Http.send(APEX_OBJECT)

10:09:42.083 (83192000)|CALLOUT_REQUEST|[38]|System.HttpRequest[Endpoint=http://testing.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest USERID="801CL"><Address ID="0"><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>, Method=GET]

10:09:42.327 (327010000)|CALLOUT_RESPONSE|[38]|System.HttpResponse[Status=OK, StatusCode=200]

10:09:42.327 (327083000)|SYSTEM_METHOD_EXIT|[38]|System.Http.send(APEX_OBJECT)

10:09:42.327 (327137000)|SYSTEM_METHOD_ENTRY|[39]|System.HttpResponse.getBody()

10:09:42.327 (327212000)|SYSTEM_METHOD_EXIT|[39]|System.HttpResponse.getBody()

10:09:42.327 (327250000)|SYSTEM_METHOD_ENTRY|[39]|System.debug(ANY)

10:09:42.327 (327264000)|USER_DEBUG|[39]|DEBUG|USPS http request: <?xml version="1.0"?><Error><Number>-2147219093</Number><Source>;SOLServerTest.Verify_Respond</Source><Description>Error Loading XML:  A name contained an invalid character.

</Description><HelpFile></HelpFile><HelpContext></HelpContext></Error>

 

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

I haven't gotten around to posting the custom component but I have posted the code you were looking for here.

All Answers

colemabcolemab

I know this is an older thread but I also ran into this while testing.  The issue is that your address isn't being encoded in UTF-8.

 

You need to investigate using the EncodingUtil.urlEncode method on your URL prior to making the call out.

Stefan Byrd-KruegerStefan Byrd-Krueger

If I can ask a follow-up, I've been playing with urlEncode, but am not certain how much of the string that feeds into setEndpoint to encode... just the xml? just the address info within the xml? Everything after http://? How much do you feed through the encoder?

 

Thanks for the tip.

colemabcolemab

Everything to the right of the XML= needs to be encoded.  However, the USPS example will have %20 in between the USERID and the opening XML tag.  In your string, take this out and replace it with a space prior to running the encoding method.  This sould resolve the issue.

 

I am working on a custom visual force component to validate USPS addresses.  When it is finished, I will post example code on my blog and post a link here.   Since I am much futher down this road, you may want to wait until my code is posted so you can copy, pate, and edit for your needs.

 

colemabcolemab

I haven't gotten around to posting the custom component but I have posted the code you were looking for here.

This was selected as the best answer
Stefan Byrd-KruegerStefan Byrd-Krueger

Many thanks! 1000 karma points to the man who shares his code. Well written, good inline comments, and helped me solve!

 

Read through your blog and my code matches up very closely. I've had luck accessing the API, including a host of error messages including the invalid character one described by the OP above. UTF-8 encoding cleared that error, but I was trying to confirm returned address details with .getBody on the HTTPResponse. All getBody delivered was the first <xml> tag, and nothing of the actual infomation in the response xml that followed.

 

All I needed to do was use the XmlStreamReader as you do in your blog. Rookie mistake, but very frustrating!

 

Thanks again for leading me in the right direction.

colemabcolemab

Glad you got it working.   Once I iron out some details, I hope to post a  VF component for mass re-use later.  So keep an eye on my blog :)

colemabcolemab

I have posted the component here.