Des-API-User
Des-API-User

Reputation: 11

Microsoft Graph (OneDrive) API - Resumable Upload Content-Type

I am trying to create the upload PUT request for the OneDrive API. It's the large file "resumable upload" version which requires the createUploadSession.

I have read the Microsoft docs here: As a warning the docs are VERY inaccurate and full of factual errors...

The docs simply say:

PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Content-Length: 26
Content-Range: bytes 0-25/128

<bytes 0-25 of the file>

I am authenticated and have the upload session created, however when I pass the JSON body containing my binary file I receive this error:

{ "error": { "code": "BadRequest", "message": "Property file in payload has a value that does not match schema.", .....

Can anyone point me at the schema definition? Or explain how the JSON should be constructed?

As a side question, am I right in using "application/json" for this at all? What format should the request use?

Just to confirm, I am able to see the temp file created ready and waiting on OneDrive for the upload, so I know I'm close.

Thanks for any help!

Upvotes: 1

Views: 1485

Answers (1)

Noam
Noam

Reputation: 1804

If you're uploading the entire file in a single request then why do you use upload session when you can use the simple PUT request?

url = https://graph.microsoft.com/v1.0/{user_id}/items/{parent_folder_ref_id}:/{filename}:/content and "Content-Type": "text/plain" header and in body simply put the file bytes.

If for some reason I don't understand you have to use single-chunk upload session then:

  1. Create upload session (you didn't specified any problems here so i'm not elaborating)

  2. Get uploadUrl from createUploadSession response and send PUT request with the following headers:

    2.1 "Content-Length": str(file_size_in_bytes)

    2.2 "Content-Range": "bytes 0-{file_size_in_bytes - 1}/{file_size_in_bytes}"

    2.3 "Content-Type": "text/plain"

  3. Pass the file bytes in body.

Note that in the PUT request the body is not json but simply bytes (as specified by the content-type header.

Also note that max chuck size is 4MB so if your file is larger than that, you will have to split into more than one chunks.

Goodlcuk

Upvotes: 2

Related Questions