Friday 20 September 2019

RestTemplate: POST Request Example


Below snippet performs POST request.
public static ResponseEntity<String> post(String url, Map<String, String> headers, String json) {
 if (url == null || url.isEmpty()) {
  return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("URL must not be null");
 }

 HttpHeaders httpHeaders = getHeadersFromMap(headers);

 HttpEntity<Object> httpEntity = new HttpEntity<Object>(json, httpHeaders);

 return REST_TEMPLATE.exchange(url, HttpMethod.POST, httpEntity, String.class);

}


You can get complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment