Onedrive API File Upload Using Java

I am trying to upload a file to a OneDrive folder using a POST REST call. My app can communicate with OneDrive. The answer I get saysThe request entity body isn't a valid JSON object.

Below is my code, please let me know the wrong part of the code or my approach.

public static void onedriveFileUpload() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost uploadFile = new HttpPost("");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        uploadFile.addHeader("Authorization", "Bearer access_token");
        builder.addPart("file", new FileBody(new File("Test.txt"), ContentType.APPLICATION_OCTET_STREAM, "Test.txt"));

        Charset chars = Charset.forName("utf-8");
        uploadFile.setHeader("Content-Type", "multipart/form-data");
        uploadFile.setHeader("charset", "UTF-8");
        uploadFile.setHeader("boundary", "AaB03x");
        HttpResponse response = null;
    try {
        response = httpClient.execute(uploadFile);
        HttpEntity responseEntity = response.getEntity();
        String json = EntityUtils.toString(responseEntity);
    } catch (Exception e) {
        // TODO Auto-generated catch block



Here is the Json response I get from OneDrive.

   "error": {
      "code": "request_body_invalid", 
      "message": "The request entity body isn't a valid JSON object."



source to share

2 answers

If you want to use the POST method, you need to set POST to /

. Your code is missing the part /files

after the folder id. If you still have problems, this will show you what the actual HTTP request would look like.



You should send a message to [ . : format] . And instead of MultipartEntity, try using ByteArrayEntity. Example:

public Photo uploadPhoto(String accessToken, String albumId, String format, byte[] bytes) throws IOException {
    Photo newPhoto = null;
    URI uri;
    try {
        uri = new URIBuilder().setScheme(DEFAULT_SCHEME)
                              .setPath("/" + albumId + "/files/" + 
                                       UUID.randomUUID().toString() + "." + format)
                              .addParameter("access_token", accessToken)
                              .addParameter("downsize_photo_uploads", "false")
    } catch (URISyntaxException e) {
        throw new IllegalStateException("Invalid album path");

    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpPut httpPut = new HttpPut(uri);
        ByteArrayEntity imageEntity = new ByteArrayEntity(bytes);
        Map<Object, Object> rawResponse = httpClient.execute(httpPut, new SomeCustomResponseHandler());
        if (rawResponse != null) {
            newPhoto = new Photo();
            newPhoto.setName((String) rawResponse.get("name"));
            newPhoto.setId((String) rawResponse.get("id"));
            // TODO:: Do something else.
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    } finally {

    return newPhoto;


This code is a snippet from OneDrive4J. Check it out at . I built this earlier this year when I was trying to search for OneDrive Java Client.



All Articles