Skip to main content
Question

API error while trying to upload File

  • May 22, 2025
  • 7 replies
  • 64 views

Forum|alt.badge.img

Using the Java SDK I'm trying to upload a test file but getting the below API exception

 

Exception in thread "main" com.box.sdk.BoxAPIException: The API returned an error code: 400
at com.box.sdk.BoxAPIResponse.(BoxAPIResponse.java:70)
at com.box.sdk.BoxJSONResponse.(BoxJSONResponse.java:30)
at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:435)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:221)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:196)
at com.box.sdk.BoxFolder.uploadFile(BoxFolder.java:468)
at com.box.sdk.BoxFolder.uploadFile(BoxFolder.java:415)
at com.mc.invoicer.boxpoc.BoxFileUpload.uploadFile(BoxFileUpload.java:76)
at com.mc.invoicer.boxpoc.BoxFileUpload.main(BoxFileUpload.java:48)

 

I'm able to successfully authenticate the user and also create a folder but unable to upload a file. Below is my Java code

 

public static void main(String[] args) throws Exception {

        String path = new File("").getAbsolutePath();
        Reader reader = new FileReader(path.concat("/").concat("src/main/resources/config.json"));
        BoxConfig boxConfig = BoxConfig.readFrom(reader);

        JWTEncryptionPreferences encryptionPref = new JWTEncryptionPreferences();
        encryptionPref.setPublicKeyID(boxConfig.getJWTEncryptionPreferences().getPublicKeyID());
        encryptionPref.setPrivateKey(boxConfig.getJWTEncryptionPreferences().getPrivateKey());
        encryptionPref.setPrivateKeyPassword(boxConfig.getJWTEncryptionPreferences().getPrivateKeyPassword());
        encryptionPref.setEncryptionAlgorithm(EncryptionAlgorithm.RSA_SHA_256);

        IAccessTokenCache accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES);

        BoxDeveloperEditionAPIConnection api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(
                boxConfig.getEnterpriseId(), boxConfig.getClientId(), boxConfig.getClientSecret(), encryptionPref, accessTokenCache);

        BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
        System.out.format("Welcome, %s!\n\n", userInfo.getName());

        BoxFolder rootFolder = BoxFolder.getRootFolder(api);
        Iterable infoIterable = rootFolder.getChildren();
        Iterator iterator = infoIterable.iterator();
        while (iterator.hasNext()) {
            BoxItem.Info info = iterator.next();
            if (info instanceof BoxFolder.Info) {
                BoxFolder.Info folderInfo = (BoxFolder.Info) info;
                uploadFile(path.concat("/").concat(FILE), api, folderInfo.getResource());
            }
            System.out.println("Item name is " + info.getName());
        }

        System.out.println("folder owner: " + rootFolder.getInfo().getOwnedBy().getName());
    }

   private static String uploadFile(String pathFileName, BoxAPIConnection api, BoxFolder folder) {
        boolean fileExists = false;
        String fileId = null;

        try {
            String fileName = pathFileName.substring(pathFileName.lastIndexOf("/")+1, pathFileName.length());

            for (BoxItem.Info itemInfo : folder) {
                if (itemInfo instanceof BoxFile.Info) {
                    BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
                    if (fileName.equals(fileInfo.getName())) {
                        fileExists = true;
                        fileId = fileInfo.getID();
                    }
                }
            }

            if (!fileExists) {
                System.out.println("uploading new file: " + fileName);
                FileInputStream stream = new FileInputStream(pathFileName);
                BoxFile.Info boxInfo = folder.uploadFile(stream, pathFileName);
                fileId = boxInfo.getID();
                stream.close();
            }
            else {
                System.out.println("uploading new version of file: " + fileName);
                BoxFile file = new BoxFile(api, fileId);
                FileInputStream stream = new FileInputStream(pathFileName);
                file.uploadVersion(stream);
                stream.close();
            }
        }
        catch (IOException e) {
            System.out.println(e);
        }
        return fileId;
    }
}

7 replies

Forum|alt.badge.img

Hi akubatoor,

 

Would you happen to have the error message that's sent with the 400 status code in the Box API response? That'll help narrow down the reason for the 400.

 

Thanks,

Jon


Forum|alt.badge.img

The response is 400 item_name_invalid

 


Forum|alt.badge.img

From https://community.box.com/t5/Managing-Content-Troubleshooting/File-and-Folder-Upload-Issues/ta-p/306

 

Verify that the file's name is valid. Box only supports file or folder names that are 255 characters or less. File names containing non-printable ascii, "/" or "\", names with leading or trailing spaces, and the special names “.” and “..” are also unsupported.

 


Forum|alt.badge.img

I don't think I'm giving any invalid file name. I'm trying to upload a file with the name taxdoc.txt. Below the API response I'm seeing

 

{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value '{\"name\":\"taxdoc.txt\", \"parent\":{\"id\":\"0\"}'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"***number removed for privacy***597673fe85807"}

Forum|alt.badge.img

I just ran your code and made one change and it seems to work fine.

I changed pathfileName to fileName in the method call folder.uploadFile()

 

if (!fileExists) {
    System.out.println("uploading new file: " + fileName);
    FileInputStream stream = new FileInputStream(pathFileName);
    BoxFile.Info boxInfo = folder.uploadFile(stream, fileName);  // <- this is just the name and not the full path
    fileId = boxInfo.getID();
    stream.close();
}

 

 


Forum|alt.badge.img

Yeah right..I also had changed the code a little bit to get this working. I changed it something like below

 

if (!fileExists) {
                FileInputStream stream = new FileInputStream(pathFileName);
                FileUploadParams fileUploadParams = new FileUploadParams();
                fileUploadParams.setName();
                fileUploadParams.setContent(stream);
                BoxFile.Info boxInfo = folder.uploadFile(fileUploadParams);
                fileId = boxInfo.getID();
                stream.close();
            }

 


Forum|alt.badge.img

Hey,

 

what is the value FILE inside concat method/ where do you initialize it ?

 

 

Thanks,

limanp