@SuppressWarnings("unchecked") public String postJSON(String url, String jsonData, Map<String, String> headers) throws URISyntaxException, ParseException { HttpResponse<JsonNode> httpResponse = null; if (!validatorUtil.isHttpURLValid(url)) { throw new URISyntaxException(url, "The URL is not absolute"); } if (!validatorUtil.isJSONValid(jsonData)) { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN); } try { httpResponse = Unirest.post(url).headers(headers).body(jsonData).asJson(); } catch (UnirestException e) { LOGGER.error("Exception occured while making post call"); JSONObject errorObject = new JSONObject(); errorObject.put("status", "500"); errorObject.put("message", e.getLocalizedMessage()); return errorObject.toJSONString(); } return httpResponse.getBody().toString(); }
@SuppressWarnings("unchecked") public String getJSON(String url, Map<String, Object> queryParams) throws URISyntaxException { HttpResponse<JsonNode> httpResponse = null; if (!validatorUtil.isHttpURLValid(url)) { throw new URISyntaxException(url, "The URL is not absolute"); } try { httpResponse = Unirest.get(url).queryString(queryParams).asJson(); } catch (UnirestException e) { LOGGER.error("Exception occured while making get call"); JSONObject errorObject = new JSONObject(); errorObject.put("status", "500"); errorObject.put("message", e.getLocalizedMessage()); return errorObject.toJSONString(); } return httpResponse.getBody().toString(); }
@SuppressWarnings("unchecked") public String delete(String url, Map<String, String> headers) throws URISyntaxException { HttpResponse<String> httpResponse = null; if (!validatorUtil.isHttpURLValid(url)) { throw new URISyntaxException(url, "The URL is not absolute"); } try { httpResponse = Unirest.delete(url).headers(headers).asString(); } catch (UnirestException e) { LOGGER.error("Exception occured while making post call"); JSONObject errorObject = new JSONObject(); errorObject.put("status", "500"); errorObject.put("message", e.getLocalizedMessage()); return errorObject.toJSONString(); } return httpResponse.getStatusText(); }