You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

varunvyas5678's avatar
varunvyas5678
Explorer | Level 3
3 years ago

Error : POST https://content.dropboxapi.com/2/files/upload_session/append_v2 400 (Bad Request)

Getting error while appending blobs to session using 'https://content.dropboxapi.com/2/files/upload_session/append_v2' in Salesforce LWC JS:

 

    uploadFile(event){

        var file = event.target.files[0];
        console.log(file);
        var dropboxToken = 'AccessToken'
        console.log('FileName:'+file.name);


       




        const maxBlob = 8 * 1000 * 1000;
                var workItems = [];
                var offset = 0;
                var sessionID;

                // Slice the file into chunks for upload session
                while (offset < file.size) {
                    var chunkSize = Math.min(maxBlob, file.size - offset);
                    workItems.push(file.slice(offset, offset + chunkSize));
                    offset += chunkSize;
                }

                const task = workItems.reduce((acc, blob, idx, items) => {
                    if (idx == 0) {
                        // Start multipart upload of file
                        return acc.then(function() {
                            return fetch(dbxURL_start, {
                                        "method": "post",
                                        "headers": {
                                        "Authorization" : "Bearer "+dropboxToken,
                                        "Content-Type": "application/octet-stream",
                                        "Dropbox-API-Arg": "{\"close\":false}"
                                        },
                                    "body": blob
                            })
                            .then((response) => {
                                sessionID = response.session_id;
                            })                                      
                            .catch(err => console.log("[ERROR] UPLOAD_SESSION_START : " + err));
                        })
                    } else if (idx < items.length-1) {
                        // Append part to the upload session
                        return acc.then(function(sessionID) {

                            return fetch(dbxURL_append, {
                                        "method": "post",
                                        "headers": {
                                        "Authorization" : "Bearer "+dropboxToken,
                                        "Content-Type": "application/octet-stream",
                                        "Dropbox-API-Arg": "{\"cursor\": {\"session_id\": \"" + sessionID + "\",\"offset\":\"" + (idx * maxBlob) + "\"},\"close\":false}"
                                        },
                                    "body": blob
                            })
                            .then(() => sessionID)
                            .catch(err => console.log("[ERROR] UPLOAD_SESSION_APPEND : " + err));
                        })
                    } else {
                        // Last chunk of data, close upload session
                        return acc.then(function(sessionID) {

                            return fetch(dbxURL_finish, {
                                        "method": "post",
                                        "headers": {
                                        "Authorization" : "Bearer "+dropboxToken,
                                        "Content-Type": "application/octet-stream",
                                        "Dropbox-API-Arg": "{\"cursor\": {\"session_id\": \"" + sessionID + "\",\"offset\":\"" + (file.size - blob.size) + "\"},\"commit\": {\"path\":\"/render/" + filename + "\",\"mode\":{\".tag\":\"add\"}}, \"close\":true}"
                                        },
                                    "body": blob
                            })
                            .then((httpResponse) => {
                                if (httpResponse.ok){
                                    console.log("HTTP RES : OK")
                                    return httpResponse.ok
                                } else {
                                    console.log("HTTP RES : NOT OK")
                                    return !httpResponse.ok
                                }                                  
                            })
                            .catch(err => console.log("[ERROR] UPLOAD_SESSION_FINISH : " + err));
                        })
                    }
                }, Promise.resolve());


                task.then(function(result) {
               
                console.log('result:',result);

            }).catch(function(error) {
                console.error(error);
            });

    }
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Make sure you check the response body itself. It should contain a more specific error indicating what the issue is.

    • varunvyas5678's avatar
      varunvyas5678
      Explorer | Level 3

      Response for the callout is:
      Error in call to API function "files/upload_session/append:2": The given OAuth 2 access token is malformed.

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        That error message indicates that there's an issue with the access token value you're supplying with the API call. Check the access token you're sending and make sure you're using the exact value as it was provided to you by Dropbox. Don't add or remove any characters, make sure there's no stray whitespace, etc. (As a matter of security, do not share any access tokens or refresh tokens here.)

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,883 PostsLatest Activity: 8 hours ago
326 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!