Closed
Description
With the latest version of RestSharp (105.2.2), I ran into an issue uploading a file with the following error message:
Error: System.Net.WebException: Request was cancelled. ---> System.IO.IOException: Cannot close the stream until all bytes are written
--- End of inner exception stack trace ---
at System.Net.WebConnectionStream.Close () [0x00183] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/mcs/class/System/System.Net/WebConnectionStream.cs:821
at System.IO.Stream.Dispose () [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr3/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.3/external/referencesource/mscorlib/system/io/stream.cs:260
at RestSharp.Http.WriteRequestBody (System.Net.HttpWebRequest webRequest) [0x00000] in <filename unknown>:0
at RestSharp.Http.PostPutInternal (System.String method) [0x00000] in <filename unknown>:0
at RestSharp.Http.AsPost (System.String httpMethod) [0x00000] in <filename unknown>:0
at RestSharp.RestClient.DoExecuteAsPost (IHttp http, System.String method) [0x00000] in <filename unknown>:0
at RestSharp.RestClient.Execute (IRestRequest request, System.String httpMethod, System.Func`3 getResponse) [0x00000] in <filename unknown>:0
Using an older version of RestSharp (105.1.0), I was able to upload the file without issue. Here is the code to repeat the issue.
using System;
using RestSharp;
using RestSharp.Extensions;
using System.IO;
namespace UploadTest
{
class MainClass
{
public static void Main (string[] args)
{
var client = new RestClient("http://petstore.swagger.io/v2/");
var request = new RestRequest("pet/{petId}/uploadImage", Method.POST);
request.AddUrlSegment("petId", "2"); // replaces matching token in request.Resource
FileStream stream = new FileStream ("/var/tmp/1.txt", FileMode.Open);
var fileParam = FileParameter.Create("file", stream.ReadAsBytes(), Path.GetFileName(((FileStream)stream).Name));
request.AddFile(fileParam.Name, fileParam.Writer, fileParam.FileName, fileParam.ContentType);
// execute the request
IRestResponse response = client.Execute(request);
if (response.ErrorException != null)
{
Console.WriteLine("Error: " + response.ErrorException);
}
Console.WriteLine ("content=" + response.Content);
}
}
}
Has anyone run into similar issue?