Issue uploading files (sync)
wing328 opened this issue · 11 comments
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?
I'have the same issue
I've found the issue too.
The issue seems to be that the file parameter is added without setting the content length:
https://github.com/restsharp/RestSharp/blob/master/RestSharp/RestRequest.cs#L179
@dgreenbean thanks for filing a PR to address the issue.
Thanks for the PR on this as well. I'll get it reviewed an merged in ASAP.
out of curiosity, what framework editions are people running into this issue with?
I'm using Mono 4.0.3
I'm using .NET 4.0 and .NET 4.5.
@hallem is there anything we (community) can do to get the fix (PR769) merged into master?
I have this issue. Did this get resolved? I always download the latest nuget package through visual studio, but I don't have the updated method signature with the content length in it.
It is right there:
public IRestRequest AddFile(string name, Action<Stream> writer, string fileName, long contentLength,
string contentType = null)