Repository navigation
Issue uploading files (sync) #742
Copy link
Copy link
Closed
Labels
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?
Reactions are currently unavailable
Activity
acesyde commentedon Oct 14, 2015
I'have the same issue
diebuc commentedon Oct 14, 2015
I've found the issue too.
dgreenbean commentedon Oct 22, 2015
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
wing328 commentedon Oct 28, 2015
@dgreenbean thanks for filing a PR to address the issue.
hallem commentedon Nov 2, 2015
Thanks for the PR on this as well. I'll get it reviewed an merged in ASAP.
hallem commentedon Nov 2, 2015
out of curiosity, what framework editions are people running into this issue with?
wing328 commentedon Nov 8, 2015
I'm using Mono 4.0.3
dgreenbean commentedon Nov 9, 2015
I'm using .NET 4.0 and .NET 4.5.
Reverted RestSharp back to 105.1.0 due to bug
wing328 commentedon Feb 17, 2016
@hallem is there anything we (community) can do to get the fix (PR769) merged into master?
seanobr commentedon Nov 28, 2016
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.
alexeyzimarev commentedon Jan 21, 2019
It is right there: