I want to know if there is an error response when sending the REST POST request, and i want to print the error as "output" in my Java application.
How do i do this?
Here is the code I'm using:
HttpClient client = new HttpClient();
try {
HttpPost request = new HttpPost("example.com/api/deposit");
StringEntity params;
params = new StringEntity("{"
+ "\"locale\": \"" + exampleclass.getLocale() + "\","
+ "\"dateFormat\": \"" + exampleclass.getDateFormat() + "\","
+ "\"transactionDate\": \"" + exampleclass.getTransactionDate() + "\","
+ "\"transactionAmount\": \"" + exampleclass.getTransactionAmount() + "\","
+ "}");
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept-Language", "en-US,en;q=0.8");
request.addHeader("Authorization", "Basic somecode&&!!");
request.setEntity(params);
HttpResponse response = client.execute(request);
//handle the response somehow
//example : System.out.println (errormessage);
} catch (Exception ex) {
ex.printStackTrace();
ex.getMessage();
} finally {
client.getConnectionManager().shutdown();
}
Any help is greatly appreciated!