Support Topics Documentation Slack YouTube Blog

Backendless throwing “File content is absent in request body”

I need to upload a file to backendless and have tried these two approaches:

let formData = new FormData();
      formData.append(path, file, path+'.mp3');
      fetch("https://api.backendless.com/E66CB437-EB54-734A-FF68-4D89CAEBFD00/D38A1511-013A-AF1B-FF23-A015C6A0B600/files/recordings/ ", {
        method: 'POST', 
        body: formData
      }).then(res => res.json())
      .then(response => console.log('Success:', JSON.stringify(response)))
      .catch(error => console.error('Error:', error));

I’ve also tried using the backendless JS API:

      Backendless.Files.upload( file, 'recordings' ).then( function( fileURL ) {
        console.log(fileURL);
        Backendless.Persistence.of("RecordingsFile").save({File:fileURL}).then((fileEntry)=>{
          //Make relation to created recording
          Backendless.Persistence.of("RecordingsFile").setRelation( fileEntry, 'recordings:Recordings:1', [saved]  );
        })
      }).catch( function( error ) {
        console.log(error)
      });

Both have failed to work and throw this error:

  Success: {"code":6011,"message":"Corrupted multipart request. File content is absent in request body","errorData":{}}

I know it’s not the file been undefined as I print it just before the request and get a file Object output. I’ve also tried creating a file object in code and using that to no avail.

Thanks for any help! Ed.

Hi, Edward!

Are you sure the file path/URI is valid? Try to get and check the file content.

Regards,
Maksym

Yeah, I tried creating a file in JS just before to no avail. Thanks, Ed.

Hi Edward

here is an example for uploading files https://github.com/Backendless/JS-SDK/tree/master/examples/file-service
you can try it out, do not forget to specify your own appId and apiKey there

Regards, Vlad

1 Like

Yeah, I’m using the Backendless.Files.upload API function as that does but I can’t get it to work.

how/where do you get your “file” for uploading, are you sure everything is ok with this file?
did you run example what I shared in the previous post? y

1 Like

Yeah, I’m getting a 400 bad request error?

Hello Edward,

Let’s clarify the things step by step. Here’s a minified version of the code you provided above, with non-important parts removed:

Backendless.Files.upload( file, 'recordings' ).then( function( fileURL ) {
        console.log(fileURL);
      }).catch( function( error ) {
        console.log(error)
      });

I’ve got a few questions regarding the results you get:

  1. Does the request succeed or fail? I.e. do you get a fileURL printed or error?
  2. How do you form the file object?
  3. If the method succeeds, do you see the uploaded file in your File Service under the recordings directory?
1 Like

1.I get an error with and a 400 request error
2.I form the file object as below:
I’ve also tried having the file deligate to the File prototype.

window.resolveLocalFileSystemURL(file.src, function (fileEntry) {
  console.log(fileEntry);
    fileEntry.file(function (file) {
      //Upload file
      console.log(file);
      Backendless.Files.upload( file, 'recordings' ).then( function( fileURL ) {
        console.log(fileURL);
        Backendless.Persistence.of("RecordingsFile").save({File:fileURL}).then((fileEntry)=>{
          //Make relation to created recording
          Backendless.Persistence.of("RecordingsFile").setRelation( fileEntry, 'recordings:Recordings:1', [saved]  );
        })
      }).catch( function( error ) {
        console.log(error)
      });
    });
});

3.I cannot get it to succeed.

The code does however work when I use a HTML file input.

I’ve got it working by using HTML file input but not when I create files in JS which is what I need.

Is it a code you run in browser or some cross-platform code you will run on mobile devices?

1 Like

It’s run in cordova so on mobile devices.

Yeah, so I tried the code I have in a browser and it works, so it must be something to do with cordova, any ideas?

I guess you’ll have to debug that request and see the exact REST request that is being issued. As far as I know, we have never done any thorough testing on Cordova, so can’t say much about it.