0

I'm getting the following exception org.springframework.web.client.HttpClientErrorException: 400 Bad Request

on this statement final ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:12001/api/profiles/bymsisdn/0747894146", String.class);

But I'm getting successful response using curl url -X GET "http://localhost:8001/api/profiles/bymsisdn/0747894146"

Extra notes: resttemplate = new RestTemplate(); // No Headers or extra convertors added as I think it's not required because using curl works fine

Srikanth Malyala
  • 941
  • 15
  • 24
  • 1
    this answer might help; http://stackoverflow.com/questions/22826887/spring-resttemplate-http-post-with-parameters-cause-400-bad-request-error – erolkaya84 May 04 '16 at 11:47

3 Answers3

1

You can use requestbin to test how your http client performs.

I think RestTemplate is putting Accept header for which your server responds 400. So you may need to edit your request headers:

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
restTemplate.exchange("http://localhost:12001/api/profiles/bymsisdn/0747894146", HttpMethod.GET, entity, String.class);

Note that this is a tentative solution, adapt to your server.

bekce
  • 3,782
  • 29
  • 30
0

curl uses / as the Accept header when you do not specify one. With RestTemplate if you do not specify the accept header , it attempts to find a suitable MIME type to your response type (in your case String). For String class depending on your configuration can match text/* MIME types which might not match what the target endpoint produces.

Try to explictly specify the content type that you need to handle using the exchange overloaded methods of RestTemplate

ekem chitsiga
  • 5,523
  • 2
  • 17
  • 18
-2

final String uri = http://localhost:8088/myapp/{data}";

RestTemplate restTemplate = new RestTemplate();

MyResponse result = restTemplate.getForObject(uri,MyResponse.class,valData);

Sumeet Tiwari
  • 343
  • 2
  • 11