Source:
public String postHttp( @CliOption(mandatory = true, key = { "", "target" }, help = "the location to post to", unspecifiedDefaultValue = "http://localhost:9000") String target, @CliOption(mandatory = true, key = "data", help = "the payload to post") String data) { final StringBuilder buffer = new StringBuilder(); URI requestURI = URI.create(target); RestTemplate restTemplate = new RestTemplate(); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType(MediaType.TEXT_PLAIN.getType(), MediaType.TEXT_PLAIN.getSubtype(), Charset.forName("UTF-8"))); final HttpEntity<String> request= new HttpEntity<String>(data, headers); try { restTemplate.setErrorHandler(new ResponseErrorHandler() { @Override public boolean hasError(ClientHttpResponse response) throws IOException { HttpStatus status = response.getStatusCode(); return (status == HttpStatus.BAD_GATEWAY || status == HttpStatus.GATEWAY_TIMEOUT || status == HttpStatus.INTERNAL_SERVER_ERROR); } @Override public void handleError(ClientHttpResponse response) throws IOException { outputError(response.getStatusCode(), buffer); } }); outputRequest("POST", requestURI, data, buffer); ResponseEntity<String> response = restTemplate.postForEntity(requestURI, request, String.class); outputResponse(response, buffer); String status = (response.getStatusCode().equals(HttpStatus.OK) ? "Success" : "Error"); return String.format(buffer.toString() + status + " sending data '%s' to target '%s'", data, target); } catch (ResourceAccessException e) { return String.format(buffer.toString() + "Failed to access http endpoint %s", target); } catch (Exception e) { return String.format(buffer.toString() + "Failed to send data to http endpoint %s", target); } }

Target:
public String postHttp( @CliOption(mandatory = true, key = { "", "target" }, help = "the location to post to", unspecifiedDefaultValue = "http://localhost:9000") String target, @CliOption(mandatory = true, key = "data", help = "the payload to post") String data) { final StringBuilder buffer = new StringBuilder(); URI requestURI = URI.create(target); RestTemplate restTemplate = new RestTemplate(); final HttpHeaders headers = new HttpHeaders(); final MediaType mediaType = new MediaType(MediaType.TEXT_PLAIN.getType(), MediaType.TEXT_PLAIN.getSubtype(), Charset.forName("UTF-8")); headers.setContentType(mediaType); final HttpEntity<String> request = new HttpEntity<String>(data, headers); try { restTemplate.setErrorHandler(new ResponseErrorHandler() { @Override public boolean hasError(ClientHttpResponse response) throws IOException { HttpStatus status = response.getStatusCode(); return (status == HttpStatus.BAD_GATEWAY || status == HttpStatus.GATEWAY_TIMEOUT || status == HttpStatus.INTERNAL_SERVER_ERROR); } @Override public void handleError(ClientHttpResponse response) throws IOException { outputError(response.getStatusCode(), buffer); } }); outputRequest("POST", requestURI, mediaType, data, buffer); ResponseEntity<String> response = restTemplate.postForEntity(requestURI, request, String.class); outputResponse(response, buffer); String status = (response.getStatusCode().equals(HttpStatus.OK) ? "Success" : "Error"); return String.format(buffer.toString() + status + " sending data '%s' to target '%s'", data, target); } catch (ResourceAccessException e) { return String.format(buffer.toString() + "Failed to access http endpoint %s", target); } catch (Exception e) { return String.format(buffer.toString() + "Failed to send data to http endpoint %s", target); } }