Skip to content

Issue uploading files (sync) #742

Closed
Closed

Description

@wing328

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?

Activity

  1. acesyde commented on Oct 14, 2015

    @acesyde

    I'have the same issue

  2. diebuc commented on Oct 14, 2015

    @diebuc

    I've found the issue too.

  3. dgreenbean commented on Oct 22, 2015

    @dgreenbean

    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

  4. wing328 commented on Oct 28, 2015

    @wing328
    Author

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

  5. hallem commented on Nov 2, 2015

    @hallem
    Member

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

  6. hallem commented on Nov 2, 2015

    @hallem
    Member

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

  7. wing328 commented on Nov 8, 2015

    @wing328
    Author

    I'm using Mono 4.0.3

  8. dgreenbean commented on Nov 9, 2015

    @dgreenbean

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

  9. wing328 commented on Feb 17, 2016

    @wing328
    Author

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

  10. seanobr commented on Nov 28, 2016

    @seanobr

    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.

  11. alexeyzimarev commented on Jan 21, 2019

    @alexeyzimarev
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions