title: Android Http Post to REST Api
body: <p>Hi I'm primarily an Android developer and am pretty clueless about http protocols, you help would be much appreciated!</p>
  
  <p>I am trying to make an api call to this: <a href="http://api.haikutest.com/developer/apidoc/assignments.html" rel="nofollow">http://api.haikutest.com/developer/apidoc/assignments.html</a> where I POST a file on the device.</p>
  
  <p>They give a sample request of:</p>
  
  <pre><code>POST /api/assignments/3/submit
  message[subject]=Subject+of+the+message&amp;message[body]=Some+long+text&amp;message[sent_at]=2013-07-05&amp;message[draft]=0&amp;message[assignment_id]=3&amp;files[][file]=%23%3CFile%3A0x007ff59c9c1de8%3E&amp;files[][filename]=test.pdf
  </code></pre>
  
  <p>And in the description state that the files param is:</p>
  
  <blockquote>
    <p>Array of files with each element containing a hash of filename and
    base64 encoded file</p>
  </blockquote>
  
  <p>This is the code that I'm trying right now (note: I'm using Robospice + Spring for Android):</p>
  
  <pre><code>   File file = new File(Environment.getExternalStorageDirectory() +
                  "/DCIM/Camera/1357279228432.jpg");
          Base64InputStream stream = new Base64InputStream(new FileInputStream(file), 0);
          HttpHeaders headers = new HttpHeaders();
          headers.add("x-haiku-auth", HaikuAPI.getAuthHeader());
          headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
          MultiValueMap&lt;String, Object&gt; parts = new LinkedMultiValueMap&lt;String, Object&gt;();
          parts.add("files[][file]", parts);
          HttpEntity&lt;Object&gt; request = new HttpEntity&lt;Object&gt;(stream, headers);
          UriComponentsBuilder builder = UriComponentsBuilder
                  .fromUriString(
                          "https://" + HaikuAPI.getDomain() + "/api/assignments")
                  .pathSegment(assignmentID + "", "submit")
                  .queryParam("message[subject]", "Assignment Completed")
  
                  .queryParam("message[body]", message)
                  .queryParam("message[assignment_id]", assignmentID)
                  .queryParam("files[][filename]", "test.jpg");
  
          URI url = builder.build().toUri();
  
          String response = getRestTemplate().postForObject(url, request, String.class);
          return response;
  </code></pre>
  
  <p>But I get an error saying that it can't serialize the stream to JSON. Following the API documentation do I have to place the stream as a param? Also what is the correct way to implement this? I would love if I didn't have to put a file size limit on the user, hence the streams. Though I have gotten it to work with small files encoded in Base64 and placed directly as a param...</p>
  
  <p>Any help is greatly appreciated! I will definitely not downvote comments or answers for small pointers.</p>
  
  <p><strong>Updated</strong></p>
  
  <p>Updated code, but still get another error message:</p>
  
  <pre><code>00:25:50.904 Thread-2066 An exception occurred during request network execution :Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/x-www-form-urlencoded]
  </code></pre>