Results 1 to 5 of 5

Thread: Getting null body in httpResponse

  1. #1
    Join Date
    Nov 2012
    Posts
    5

    Default Getting null body in httpResponse

    Reading my headers, it's reporting the correct number of bytes in the header. But the body is empty.

    Header:
    Code:
    <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?

    Code:
                    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);
    		}

  2. #2
    Join Date
    Nov 2010
    Posts
    175

    Default

    @kireol try setting the responseType in the call to exchange like this:

    Code:
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
    String tmp = response.getBody();
    Roy Clarkson
    Spring Mobile Projects Lead

  3. #3
    Join Date
    Nov 2012
    Posts
    5

    Default

    Thank you Roy. That worked! Usually I'm good at reading the instructions. Guess I fell a bit short here.

    Quote Originally Posted by Roy Clarkson View Post
    @kireol try setting the responseType in the call to exchange like this:

    Code:
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
    String tmp = response.getBody();

  4. #4
    Join Date
    Nov 2010
    Posts
    175

    Default

    Good to hear! The combination of responseType and available message converters are used internally to determine how and if a response can be converted.
    Roy Clarkson
    Spring Mobile Projects Lead

  5. #5
    Join Date
    Nov 2012
    Posts
    5

    Default

    That's good to know; and a great feature. springframework is a great library

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •