Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue uploading files (sync) #742

Closed
wing328 opened this issue Aug 25, 2015 · 11 comments
Closed

Issue uploading files (sync) #742

wing328 opened this issue Aug 25, 2015 · 11 comments
Labels

Comments

@wing328
Copy link

wing328 commented Aug 25, 2015

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?

@acesyde
Copy link

acesyde commented Oct 14, 2015

I'have the same issue

@diebuc
Copy link

diebuc commented Oct 14, 2015

I've found the issue too.

@dgreenbean
Copy link

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
Copy link
Author

wing328 commented Oct 28, 2015

@dgreenbean thanks for filing a PR to address the issue.

@hallem
Copy link
Member

hallem commented Nov 2, 2015

Thanks for the PR on this as well. I'll get it reviewed an merged in ASAP.

@hallem
Copy link
Member

hallem commented Nov 2, 2015

out of curiosity, what framework editions are people running into this issue with?

@hallem hallem added the bug label Nov 3, 2015
@wing328
Copy link
Author

wing328 commented Nov 8, 2015

I'm using Mono 4.0.3

@dgreenbean
Copy link

I'm using .NET 4.0 and .NET 4.5.

@wing328
Copy link
Author

wing328 commented Feb 17, 2016

@hallem is there anything we (community) can do to get the fix (PR769) merged into master?

@seanobr
Copy link

seanobr commented 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
Copy link
Member

It is right there:

        public IRestRequest AddFile(string name, Action<Stream> writer, string fileName, long contentLength,
            string contentType = null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants