Announcement Announcement Module
Collapse
No announcement yet.
Getting null body in httpResponse Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    @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();

    Comment


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

      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();

      Comment


      • #4
        Good to hear! The combination of responseType and available message converters are used internally to determine how and if a response can be converted.

        Comment


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

          Comment

          Working...
          X