I have written code using camel to invoke USPS address validaton api. I tried all the possible way to send the request but its getting encoded and request fails. while endpoint of USPS API expecting xml - string , due to encoding of request xml endpoint unable to parse the request and getting below parsing error.
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Number>80040B19</Number>
<Description>XML Syntax Error: Please check the XML request to see if it can be parsed.</Description>
<Source>USPSCOM::DoAuth</Source>
</Error>
Request gets encoded like when checked in wireshark:
/ShippingAPITest.dll?API=Verify&XML=%3CAddressValidateRequest%20USERID=%5C%22705LBEYO2631%5C%22%3E%3CAddress%3E%3CAddress1%3E%3C/Address1%3E%3CAddress2%3E6406%20Ivy%20Lane%3C/Address2%3E%3CCity%3EGreenbelt%3C/City%3E%3CState%3EMD%3C/State%3E%3CZip5%3E?%3C/Zip5%3E%3CZip4%3E?%3C/Zip4%3E%3C/Address%3E%3C/AddressValidateRequest%3E HTTP/1.1
While its expected format is:
API=Verify&XML=<AddressValidateRequest%20USERID=\"705LBEYO2631\"><Address><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>
Route file: route.xml
<route id="UspsAddressValidationRoute" >
<from id="ProcessAddressValidationRequest" uri="direct:processUspsAddressValidation" />
<to id="PrepareAddressValidationRequest" uri="bean:serviceUspsAddressValidationProcessor?method=constructAddressValidationRequest" />
<setHeader headerName="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<to id="SendAddressValidationRequestToUsps" uri="{{usps.soapEndpoint}}"/>
<to id="ProcessAddressValidationUspsResponse" uri="bean:serviceUspsAddressValidationProcessor?method=processUspsAddressValidationResponse" />
</route>
usps.soapEndpoint = http://testing.shippingapis.com/ShippingAPITest.dll
ServiceUspsAddressValidationProcessor --> ConstructAddressValidationRequest()
Method has below snippets of code:
exchange.setProperty(Exchange.HTTP_METHOD, "GET");
//exchange.setProperty(Exchange.CONTENT_TYPE, "text/xml");
exchange.getIn().setHeader(Exchange.HTTP_QUERY, getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setHeader(Exchange.HTTP_QUERY, getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setBody("");
...
public String getHttpQuery(UspsAddress uspsAddress){
String validateUrl = "API=Verify&XML=<AddressValidateRequest USERID=\\\"%s\\\"><Address><Address1>%s</Address1><Address2>%s</Address2><City>%s</City><State>%s</State><Zip5>%s</Zip5><Zip4>%s</Zip4></Address></AddressValidateRequest>";
String userid = "XXXXXXXXXXXX" ;
String httpQuery = String.format(validateUrl, userid,
uspsAddress.getAddress1(),
uspsAddress.get_Address2(),
uspsAddress.getCity(),
uspsAddress.getState(),
uspsAddress.getZip(),
uspsAddress.getZipPlus4());
//httpQuery = StringEscapeUtils.escapeJava(httpQuery);
System.out.println("\n****************httpQuery ****************\n" + httpQuery);
return httpQuery;
}