File upload: '400' [HTTP 400] <bad request>

I’m getting the following error when I attempt to upload a png file to the database: ‘400’ [HTTP 400] <bad request>

This functionality has been working on my app for months and I havne’t made any code changes. I did recently change my back endless setup to add a new version of the database so now i have a v1(the first version created by default which Im using as my dev environment) and a production version. Could this be related to my file upload errors? I get errors when working in both environments.
code

 func uploadImage(image: NSData, imageTitle: String, completion:(BackendlessFile?, Fault?) -> Void)
 {
 
 print("\n============ Uploading files with the ASYNC API ============")


 backendless.fileService.upload(
 "myfiles/test.png",
 content: image,
 overwrite: true,
 response: { ( uploadedFile : BackendlessFile!) -> () in
 print("File has been uploaded. File URL is - \(uploadedFile.fileURL)")
 completion(uploadedFile, nil)
 },
 error: { ( fault : Fault!) -> () in
 print("Server reported an error: \(fault)")
 completion(nil, fault)
 })
 }

I tried testing with the sample piece of code from your guide and got the same result, so this issue either must be on your end or somehow related to the db versions.

Heres the sample code that still gave me a 400 error, only change I made was to update println to print:

    func uploadAsync() {
        
        print("\n============ Uploading files with the ASYNC API ============")
        
        var data = NSData(bytes:"Hello mbaas!\nUploading files is easy!", length:37)
        backendless.fileService.upload("myfiles/myhelloworld-async.txt", content: data,
                                       response: { (var uploadedFile : BackendlessFile!) -> () in
                                        print("File has been uploaded. File URL is - \(uploadedFile.fileURL)")
            },
                                       error: { (var fault : Fault!) -> () in
                                        print("Server reported an error: \(fault)")
            }
        )
    }

Found a fix… if I use:

backendless.fileService.fileSave

instead of:

backendless.fileService.upload

it works.

Not sure what would have changed to make this happen. Would appreciate an explanation for why I needed to change the syntax - thanks!

You marked this as solved but I’d like an explanation for why i needed to make an update

Hi Chris,

We shall test both methods and notify you about the results when available. Sorry for the inconvenience!

Any updates on this? Using the previous function I was able to overwrite the existing file, the new function doesn’t allow overwrites and is causing problems in my app.

Hey guys, I’m also getting a bad request uploading a single file. Any idea about what’s is wrong here?
*Using react-native component, selecting the picture just fine…

Hi Marcio

There is should be an error message from the server, it can give us more details, can you see that?

I’m not sure but I guess there is must be FormData, but maybe axios already does it, and also according to our JS-SDK we use “file” form field for transferring a file to the server https://github.com/Backendless/JS-SDK/blob/master/src/files/send.js#L18

Can you provide a code snippet which we can use for reproducing your issue?

Regards, Vlad

Hi Vladimir, thanks for your support. As you mentioned I did some digging and I found a way to get extra message from Axios, and now I could see here is something related with React.

message: “Corrupted multipart request. Header Content-Type must be set to value multipart/form-data”

Found solution: Axios don't send form data in react-native · Issue #1321 · axios/axios · GitHub

Thanks for your help!

1 Like

Thank you for sharing the solution, I believe it will be helpful for other developers