title: Multipart File upload using Apache REST client
body: <p>I was trying to upload files to OneDrive using multipart request. I've tried many ways but none of them worked.</p>
  
  <p>The request is of the form :-</p>
  
  <pre><code>POST /drive/items/{folder-id}/children
  Content-Type: multipart/related; boundary="A100x"
  
  --A100x
  Content-ID: &lt;metadata&gt;
  Content-Type: application/json
  
  {
    "name": "newfile.txt",
    "file": {},
    "@content.sourceUrl": "cid:content",
    "@name.conflictBehavior": "rename"
  }
  
  --A100x
  Content-ID: &lt;content&gt;
  Content-Type: text/plain
  
  Contents of the file to be uploaded.
  
  --A100x--
  </code></pre>
  
  <p>I've tried many ways. The snippet I've done for this added below. Any help would be appreciated.</p>
  
  <p>Snippet :-</p>
  
  <pre><code>            HttpClient httpClient = HttpClientBuilder.create().build();
              URIBuilder uriBuilder = new URIBuilder(URI);
              HttpPost post = new HttpPost(uriBuilder.build());
              MultipartEntityBuilder builder = MultipartEntityBuilder.create();
              builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
              Charset chars = Charset.forName("utf-8");
              builder.setCharset(chars);
              post.setEntity(builder.build());
              builder.addPart("content", new FileBody(new File("/home/myfiles/test"), ContentType.APPLICATION_OCTET_STREAM, "test"));
              builder.addPart("metadata", new StringBody(metaJson, ContentType.APPLICATION_JSON));
              post.setHeader("Content-Type", "multipart/form-data");
              post.addHeader("Authorization", "Bearer " + ACCESS_TOKEN);
              try {
                  HttpResponse response = httpClient.execute(post);
                  if (response.getStatusLine().getStatusCode() == 200) {
                      br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
                      responseBuilder = new StringBuilder();
                      while ((output = br.readLine()) != null) {
                          responseBuilder.append(output);
                      }
                  } else {
                      System.out.println("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
                  }
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>URI : </p>
  
  <blockquote>
    <p><a href="https://api.onedrive.com/v1.0/drive/root:/myfiles/children" rel="nofollow">https://api.onedrive.com/v1.0/drive/root:/myfiles/children</a></p>
  </blockquote>