0

I have my below code which works proper on 106.15, I get a succesfull status. Project is Visual Studio 2022 (.Net 4.8)

accessToken = GetAccessToken()
Dim rRequest As RestRequest
Dim rClient As RestClient
Dim rResponse As RestResponse

Try
    Dim data() As Byte = File.ReadAllBytes(fileSource)
    
    rClient = New RestClient("https://graph.microsoft.com/v1.0")
    rRequest = New RestRequest(uploadURL, Method.Put)

    rRequest.AddHeader("Authorization", "Bearer " & accessToken)
    rRequest.AddHeader("Content-Range", New ContentRangeHeaderValue(0, data.Length - 1, data.Length).ToString)
    rRequest.AddHeader("Content-Length", data.Length)
    rRequest.AddParameter("application/binary", data, ParameterType.RequestBody)

    rResponse = Await rClient.ExecuteAsync(rRequest)

When I execute the code on 107.15 it fails with the following error : There was an error sending the request (translated from dutch)

Any idea what could be wrong or should be changed?

Co Akker
  • 11
  • 2

2 Answers2

1

Looks like 108.0.1 fixes the issue, it is working now.

This commit from 03 Mar 22 shows the fix.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Co Akker
  • 11
  • 2
0

Please read the documentation.

Don't use this:

rRequest.AddHeader("Content-Range", New ContentRangeHeaderValue(0, data.Length - 1, data.Length).ToString)
rRequest.AddHeader("Content-Length", data.Length)
rRequest.AddParameter("application/binary", data, ParameterType.RequestBody)

use AddFile instead.

You can still add the Content-Range header manually using AddHeader. As you aren't uploading the file (although you kind of do), you might not the file name to be there. In that case, you can try this:

rRequest
    .AddBody(data)
    .AddHeader("Content-Range", New ContentRangeHeaderValue(0, data.Length - 1, data.Length).ToString())

When AddBody gets a byte array, it will use the application/binary content type, and set the length correctly.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • Thank you Alexey, I read the documentation ofcourse before posting ;-). But AddFile does not work with the Graph API when using an upload session, the content-range header and content-length are required headers, as you can upload a file fully or parts, if they are not there then it always nag : ("{""error"":{""code"":""invalidRequest"",""message"":""The Content-Range header is missing or malformed.""}}") https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0 shows how to do it, which does work in 106.15. Downgrade to 106.15 or is it fixable? – Co Akker Apr 21 '22 at 07:32
  • The `Content-Range` header is only required if you upload a part of the file. But your code uploads the whole thing, it's really weird that it's failing. – Alexey Zimarev Apr 21 '22 at 11:08
  • The thing is that RestSharp uses `StreamContent` that _supposes_ to set all the correct headers. But again, the `Content-Range` header must not be a required header. – Alexey Zimarev Apr 21 '22 at 11:10
  • If i do only the .addbody and .addheader(Content-Range it gives this : "{""error"":{""code"":""invalidRequest"",""message"":""The Content-Range header length does not match the provided number of bytes.""}}". Is there any trick to check the set length? – Co Akker Apr 21 '22 at 15:11
  • RestSharp sets the length properly, I am not sure what happens. You can use requestbin.com and send your requests there both using RS 106 and 107 to see the difference in headers. It would give you a better idea about what's going on. – Alexey Zimarev Apr 22 '22 at 06:40
  • When using your body method on 107.3, it gives the length 26130, it should be 19594. On 106.15 that method crashes – Co Akker Apr 22 '22 at 12:13
  • With the .addfile method the length is : 19820 (106.15) – Co Akker Apr 22 '22 at 12:36
  • With the .addfile method the length is : 19802 (107.3), 19802 is no typo – Co Akker Apr 22 '22 at 12:38
  • Thanx for the tip of requestbin, really handy :-) – Co Akker Apr 22 '22 at 12:41
  • What's the actual file length then? – Alexey Zimarev Apr 22 '22 at 15:11
  • On Windows Command Prompt it show 19.594 bytes, Getting the length of the bytes in VB .Net also gives 19594 – Co Akker Apr 25 '22 at 06:55