Hi,
I'm trying to get multipart uploading working with v105.2.3, but it seems there is something wrong with the Content-Length.
using (FileStream source = File.Open(path, FileMode.Open))
{
RestSharp.RestRequest request2 = new RestSharp.RestRequest();
request.Resource = $"/files/uploads/{UploadId}";
request.Method = Method.POST;
request.AddFile(filename, source.CopyTo, filename);
request.AlwaysMultipartFormData = true;
IRestResponse<UploadResponse> response2 = this.Client.Execute<UploadResponse>(request);
if ((int)response2.StatusCode != 201)
{
throw new Exception(response2.StatusDescription);
}
}
The response2 has this Error Message (it's in german and means Request was cancelled.):
_Die Anfrage wurde abgebrochen: Die Anfrage wurde abgebrochen.._
Inner Exception (Cannot close the stream until all bytes are written):
_Stream kann nicht geschlossen werden, bevor alle Bytes geschrieben wurden._
In Fiddler I get this:
Request - Entity
Content-Length: 210
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Response
HTTP/1.1 408 Request body incomplete
The request body did not contain the specified number of bytes. Got 159, expected 210
I need to use the Nuget package with this versions since I have other packages (like RestSharp.Newtonsoft.Json) that require this version.
Seems like the same problem here:
https://github.com/restsharp/RestSharp/issues/742
Best regards,
Alex
Hi Alex,
I have the same issue. Have you solved it or worked arround it?
Best regards,
Maurice
I had this same issue and resolved it by using request.Files.Add() method instead of AddFile() like so:
request.Files.Add(new FileParameter
{
Name = "fileAttach",
Writer = (s) =>
{
FileStream stream = File.Open(_attachment.path, FileMode.Open);
stream.CopyTo(s);
stream.Dispose();
},
FileName = _attachment.name,
ContentType = _attachment.contentType,
ContentLength = _attachment.size
});
You can use AddFile
but you need to specify the content length, there is a parameter for that.
Most helpful comment
I had this same issue and resolved it by using request.Files.Add() method instead of AddFile() like so: