When I run this code, I get the response in httpResponse. It’s reporting the correct number of bytes in the header. But the body is empty. when I call .getBody() it’s null.
Header:
<200 OK,{Date=[Thu, 29 Nov 2012 16:26:06 GMT], Server=[Apache], Vary=[Accept-Encoding], Content-Length=[5072], Keep-Alive=[timeout=10, max=100], Connection=[Keep-Alive], Content-Type=[text/html]}>
What am I doing wrong?
String url = new String("http://www.myurl.com/scripts/json/v1/slipmanager.php");
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("username", userName);
formData.add("password", md5(userPassword));
formData.add("method", "getslips");
RestTemplate template = new RestTemplate(true);
template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("multipart", "form-data"));
template.getMessageConverters().add(new StringHttpMessageConverter());
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
ResponseEntity<?> httpResponse = null;
try
{
httpResponse = template.exchange(url, HttpMethod.POST, request, null);
String tmp = (String) httpResponse.getBody();
//THIS IS WHERE THE BODY IS NULL
}
catch (Exception e)
{
Log.e("POST", e.getMessage(), e);
}
You asked for a response type “null” so Spring thinks you want to discard the body. If you wanted it to be a String, String.class should work. You don’t even need the explicit StringHttpMessageConverter as far as I know.