Random error with CreateUploadSession + OneDrive for Business: The request is malformed or incorrect

12 Views Asked by At

Using MSAL 1.1.24 & making API calls in an iOS app that supports uploading to OneDrive. Everything works fine except for OneDrive Business users, who get a RANDOM error. Yes, sometimes it works.

The error is "The request is malformed or incorrect" "innerError".

The createUploadSession code is the following:

request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept") // https://stackoverflow.com/questions/72803240/error-400-invalid-request-with-upload-url-but-file-is-properly-send-to-sharepoin
// Conflict management
let fileExistBehavior = fileExistProcedure == .keepBoth ? "rename" : "replace"
let params = ["item": [ // https://techcommunity.microsoft.com/t5/microsoft-graph/cannot-get-conflict-behaviour-to-work-with-upload-to-sharepoint/m-p/1995331
              "@microsoft.graph.conflictBehavior":fileExistBehavior,
//                          "fileSize":fileSize, --> filesize not supported on business account https://learn.microsoft.com/en-us/graph/api/resources/driveitemuploadableproperties?view=graph-rest-1.0 // See "Only on OneDrive Personal"
              "name":fileName  ] // End item
             ] as [String : Any] // name must be the same as the one mentioned in the URL (in other words, the file name must be in both place)
            // Other thing to try if it still fails randomly for some users: set the conflictBehavior in the URL, not in the body (doc is unclear)
request.httpBody = try! JSONSerialization.data(withJSONObject: params, options: JSONSerialization.WritingOptions())

Info about conflict behavior: The documentation is contradictory. Sometimes it says to put the conflictBehavior in the body, sometimes in the URL: See here

What is exactly going on, why am I getting random errors, and what should I do to get it to work reliably?

Thank you very much

0

There are 0 best solutions below