I am facing an issue that when I post a json message with special char from Java program, that special character ( say ’ in O’Reilly) is replaced with ?. If I post same message from POSTMAN, I get correct response. Could you please suggest how to handle this issue ?
Thank You.
Below is the code and json I am using:
{ "lastName":"O’Reilly", "firstName":"Shaun"}
package rest_apis;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class IDQ_tranformation_cd {
public static void main(String args[]){
String json_req="{"lastName":"O’Reilly","firstName":"Shaun"}";
try{
CloseableHttpClient httpclient = HttpClients.createDefault();
String instanceUrl = "https://my-end-point";
URIBuilder builder = new URIBuilder(instanceUrl);
StringEntity params =new StringEntity(json_req);
HttpPost post_data = new HttpPost(builder.build());
post_data.addHeader("Content-Type","application/json");
post_data.addHeader("Accept", "application/json");
post_data.setEntity(params);
HttpResponse queryResponse = httpclient.execute(post_data);
int Out_RespCd = queryResponse.getStatusLine().getStatusCode();
HttpEntity httpEntity = queryResponse.getEntity();
System.out.println(Out_RespCd +":"+EntityUtils.toString(httpEntity));
} catch (Exception e) {
System.out.println("Exception :"+e.toString());
}
}
}
I expect correct output as (I am able to get this when I use postman):
{"lastName":"O’Reilly","firstName":"Shaun"}
But I am getting this when I use java code (’ replaced with ?)
{"lastName":"O?Reilly","firstName":"Shaun"}