I’m trying to upload a file from my app to OneDrive Business repository using graph API.
and I know there are two steps to do that…
Create upload session by calling - https://graph.microsoft.com/v1.0/me/drive/root:/hello.docx:/createUploadSession (this api call works fine)
Response of above API call be return a URL which I’m using to upload a file.
this is how I’m creating request for this API call -
var request = NSMutableURLRequest(url:url)
request.timeoutInterval = TimeInterval(60)
request.httpMethod = “PUT”
request.setValue(“application/json”, forHTTPHeaderField: “accept”)
uploadRequest.setValue(“26”, forHTTPHeaderField:“Content-Length”)
uploadRequest.setValue(“application/x-www-form-urlencoded”, forHTTPHeaderField:“Content-Type”)
let contentLength = Int(file.contentLength) ?? 0;
uploadRequest.setValue(“bytes 0-(25)/(contentLength)”, forHTTPHeaderField:“Content-Range”);
Note upload is implemented using URLSessionUploadTask -
self.session.uploadTask(with: request as URLRequest, fromFile: self.fileUrl)
It start the upload and file gets uploaded 100% but at last update fails with response as below
<NSHTTPURLResponse: 0x1c0a39420> { URL: https://indxyz-my.sharepoint.com/personal/ram_kapoor_indxyz_onmicrosoft_com/_api/v2.0/drive/items/01BSG3ERN6Y2GOVW7725BZO354PWSELRRZ/uploadSession%3Fguid=‘cb929739-8c1c-48b2-9be4-3dc1cd2aef09’&path=’~tmp0F_hello.docx’&overwrite=True&rename=False&dc=0&tempauth=redacted }
{ Status Code: 400, Headers {
“Content-Length” = (
0
);
Date = (
“Mon, 22 Oct 2018 10:46:09 GMT”
);
“Strict-Transport-Security” = (
“max-age=31536000”
);
microsoftsharepointteamservices = (
“16.0.0.8210”
);
“ms-cv” = (
“nprY6jvwAHAoBWzVPpyQgQ.0”
);
p3p = (
“CP=“ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI””
);
“request-id” = (
“ead89a9e-f03b-7000-2805-6cd53e9c9081”
);
sprequestguid = (
“ead89a9e-f03b-7000-2805-6cd53e9c9081”
);
“x-content-type-options” = (
nosniff
);
“x-frame-options” = (
SAMEORIGIN
);
“x-ms-invokeapp” = (
“1; RequireReadOnly”
);
“x-msedge-ref” = (
“Ref A: A4A5ACB248D94992BD7B83645B01A952 Ref B: BOM01EDGE0220 Ref C: 2018-10-22T10:46:09Z”
);
“x-powered-by” = (
“ASP.NET”
);
} }
My Android App is working fine as buffer size has been specified there - I thing this is something to do with buffer size as Microsoft has mentioned in the document here
Upload bytes to the upload session
To upload the file, or a portion of the file, your app makes a PUT request to the uploadUrl value received in the createUploadSession response. You can upload the entire file, or split the file into multiple byte ranges, as long as the maximum bytes in any given request is less than 60 MiB.
The fragments of the file must be uploaded sequentially in order. Uploading fragments out of order will result in an error.
Note: If your app splits a file into multiple byte ranges, the size of each byte range MUST be a multiple of 320 KiB (327,680 bytes). Using a fragment size that does not divide evenly by 320 KiB will result in errors committing some files.
I’m not sure which part I’m wrong at and how to provide buffer size on session upload task in iOS.
I couldn’t get the clarity from different methods. Any help is appreciated.
Thanks
source: stackoverflow