I am attempting a invoke an SMS sending API through a java client in my spring boot application. Following is the code I am using.
HashMap<String, String> requestParameters = new HashMap<>();
requestParameters.put("user_name", env.getProperty(GlobalConstants.SMSConstants.IPROMO_USERNAME));
requestParameters.put("api_key", appCtx.getBean(PropertyServiceForJasyptSimple.class).getIpromoKey());
requestParameters.put("gateway_type", GlobalConstants.SMSConstants.IPROMO_ECONOMY_GATEWAY_ID);
requestParameters.put("country_code", GlobalConstants.SMSConstants.SL_COUNTRY_CODE);
requestParameters.put("number", phoneNumber.substring(1));
String message = String.format(msgSource.getMessage(GlobalConstants.SMSConstants.SMS_PASSWORD_RECOVERY_MSG,
new Object[]{}, LocaleContextHolder.getLocale()), loginId, recoveryCode);
requestParameters.put("message", message);
try {
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(requestParameters);
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(GlobalConstants.SMSConstants.IPROMO_ENDPOINT);
request.setEntity(new StringEntity(requestBody));
HttpResponse response = client.execute(request);
System.out.println(requestBody);
System.out.println(request);
} catch (IOException e) {
e.printStackTrace();
}
Following is a sample request body that I see in my print statement.
{"country_code":"+94","number":"XXX","api_key":"XXX","user_name":"XXX","gateway_type":"1","message":"හිතවත් වරණ පරිශීලකය,\nඔබගේ පිවිසුම් හැඳුනුම්පත: XX\nඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"}
I am using ipromo sms gateway.
However, what I receive in my SMS shows ???? characters instead of unicode characters. However, when I send the following request using the Advanced Rest Client, I receive a message with proper characters.
{
"user_name": "XXX",
"api_key": "XXXX",
"gateway_type": "1",
"country_code": "94",
"number": "XXXX",
"message": "විතවත් වරණ පරිශීලකය,ඔබගේ පිවිසුම් හැඳුනුම්පත: XXඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"
}
It appears the issue is with the Java code I am using. Any idea on what is wrong with my code?
Create your entity with utf-8
in your client code.
new StringEntity(requestBody, "utf-8")