Announcement Announcement Module
No announcement yet.
Using the android rest template to make a MultiPart request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the android rest template to make a MultiPart request

    Hi all

    been at this for about a day, trying to get the resttemplate to post my multipart request.

    heres the code that im using as a template.

    RestTemplate template = new RestTemplate();
    String uri = "http://localhost:8080/multipart-http/inboundAdapter.htm";
    Resource s2logo = 
       new ClassPathResource("org/springframework/samples/multipart/spring09_logo.png");
    MultiValueMap map = new LinkedMultiValueMap();
    map.add("company", "SpringSource");
    map.add("company-logo", s2logo);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("multipart", "form-data"));
    HttpEntity request = new HttpEntity(map, headers);
    ResponseEntity<?> httpResponse =, HttpMethod.POST, request, null);
    My only differences are that i set headers for authentication and that I cant use the FileSystemResource to specifiy my file....

    everytime i execute the code i get

    04-12 02:06:37.762: ERROR/BaseListActivity(6543): Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [multipart/form-data]
    im using M2 and the file that i want to upload is not on my Classpath.

    .... id really appreciate the help

    Many thanks as allways

  • #2
    Your code requires the use of the FormHttpMessageConverter to post the form data. The FormHttpMessageConverter was not being automatically added to the MessageConverters collection when you created a new RestTemplate instance in M2. The M3 release now adds this by default. If you are on API Level 8 or higher, then it adds the XmlAwareFormHttpMessageConverter which has additional XML support in form data. I've also added an example in the showcase application that basically implements your code. The feedback is always appreciated!


    • #3
      Is there no support to parse a POJO and use it?

      I used this example now:
       MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
       parts.add("field 1", "value 1");
       parts.add("file", new ClassPathResource("myFile.jpg"));
       template.postForLocation("", parts);
      But how to fetch the Respone which is JSON in my case.

      Thanks for your help.
      Last edited by drindt; May 2nd, 2012, 02:48 AM.


      • #4
        Yes, you should be able to use one of the JSON message converters to convert the response to a POJO. Take a look at the postForObject(...) method [1] or even the exchange(...) method for doing this.

        [1] [url]
        Last edited by Roy Clarkson; May 10th, 2012, 11:36 AM. Reason: url formatting issues


        • #5
          I have the same problem.

          How do you solved it?