Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format Content-Range header for Upload Session Microsoft Graph API

I am running into an issue uploading files to an upload session using v1.0 Microsoft Graph API. My steps:

  1. Get Access Token: Success
  2. Select File / Create Upload Session: Success (I get back an uploadUrl to upload the bytes)
  3. Upload Bytes to uploadUrl: Fail with below error.

Here is the error I am receiving:

{
   "error": 
   {
      "code":"invalidRequest",
      "message":"The Content-Range header is missing or malformed."
   }
}

I have tried using the following formats and I still receive the same error.

"bytes 0-100/100"
"0-100/100"
"0-100"

I am following this article from Mircosoft https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_createuploadsession

Here is my function that uploads to the uploadUrl retreived from creating the upload session

public uploadToSession(file: HTMLInputElement, session: any, fileData: any) {
    var headers = new Headers();
    headers.append('Content-Length', fileData.length); //(ie: "100")
    headers.append('Content-Range', fileData.range); //(ie: "bytes 0-100/100")
    this.http.put(session.uploadUrl, { headers })
        .subscribe(
            (res: Response) => console.log(res),
            error => console.log(error)
        );
}

Any help is much appreciated! Thanks in advance.

like image 767
Brian Smith Avatar asked Dec 03 '22 21:12

Brian Smith


1 Answers

The Content-Range header has the following format:

Content-Range: bytes <startindex>-<endindex>/<totallength>

Your issue appears to be a mixing of indexes (0-based) and lengths (1-based). For a 100-byte file your header would need to look like:

Content-Range: bytes 0-99/100
like image 163
Brad Avatar answered Dec 25 '22 17:12

Brad