Sandesh
Sandesh

Reputation: 1220

Get value from response body in RestClient

I am using Rest client of Firefox. I want to get value from response that is showing on Response body(Raw) in Rest-Client. I want to get this value in SpringBoot. Is it possible? If yes then How? I have tried too many times but didn't get Satisfactory solution.

enter image description here

Upvotes: 1

Views: 6071

Answers (1)

Michael Hibay
Michael Hibay

Reputation: 522

Using a Spring RestTemplate to make the calls will return a ResponseEntity. The simplest way to get the raw response would be this:

RestTemplate restTemplate = new RestTemplate();
try{
  ResponseEntity<String> response = restTemplate.getForEntity(URI.create("http://example.org"),String.class);
  System.out.println(response.getBody());
} catch (RestClientResponseException exception){
  System.out.println(String.format("Error code %d : %s",e.getStatusCode().value(),e.getResponseBodyAsString()));
  HttpHeaders errorHeaders = e.getResponseHeaders();
}

The ResponseEntity class will allow you to access the headers as well.

For more information on RestTemplate you can look at the docs here.

Upvotes: 3

Related Questions