'Restsharp 107.x ExecuteAsync upload file to uploadsession fails (GraphAPI Sharepoint), 106.x does work
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
