Restsharp: RestRequest.AddFile does not work

Created on 1 Nov 2010  ·  13Comments  ·  Source: restsharp/RestSharp

seems like adding file to RestRequest doesn't work.

ResponseStatus is set as Error.

the response.Error message is "Bytes to be written to the stream exceed the Content-Length bytes size specified."

have to update the content length before writing to the the stream.

bug

Most helpful comment

This was my solution to get around this bug:

var req = GetRestRequest("Upload", Method.POST, null);
//req.AddFile("file",
//    (s) => {
//        var stream = input(imageObject);
//        stream.CopyTo(s);
//        stream.Dispose();
//    },
//    fileName, contentType);

req.Files.Add(new FileParameter {
    Name = "file",
    Writer = (s) => {
        var stream = input(imageObject);
        stream.CopyTo(s);
        stream.Dispose();
    },
    FileName = fileName,
    ContentType = contentType,
    ContentLength = contentLength
});   

All 13 comments

most likely the changes to Content-Length to work around .NET weren't tested with files and the header isn't being set right. thanks for the report.

try this again with jonfuller's latest changes and tell me if it's still not working

i believe this to be resolved. please test and notify if not.

Problem returned.
In version 105.2.3 (current stable on nuget) with file ~3.5MB in size.

After downgrading to 105.1.0 all works fine.

There is a similar post at StackOverflow (not mine).

I think this is related to another bug #769 and #742 let me get that fixed and we'll see what happens. Something happened with the content length.

There's any news about this?

bump - seems still to be a problem

yep, i've got issues with this too, even going back to 105.1.0 hasn't helped

Still not solved.

I am having a similar issue with newer versions of restsharp
when using
req.AddFile("call[picture_attributes][file]", (s) => imageStream.CopyTo(s), GetRandomFileName("jpg"), "image/jpeg");

It doesnt work in newer versions

but if i use
req.AddFile("call[picture_attributes][file]", imageStream.GetBytes(), GetRandomFileName("jpg"), "image/jpeg");

with

    public static byte[] GetBytes(this Stream s)
    {
        using (var ms = new MemoryStream())
        {
            s.CopyTo(ms);
            return ms.ToArray();
        }
    }

it works.

so using a stream by using imageStream.CopyTo doesnt work while sending byte[] works

This was my solution to get around this bug:

var req = GetRestRequest("Upload", Method.POST, null);
//req.AddFile("file",
//    (s) => {
//        var stream = input(imageObject);
//        stream.CopyTo(s);
//        stream.Dispose();
//    },
//    fileName, contentType);

req.Files.Add(new FileParameter {
    Name = "file",
    Writer = (s) => {
        var stream = input(imageObject);
        stream.CopyTo(s);
        stream.Dispose();
    },
    FileName = fileName,
    ContentType = contentType,
    ContentLength = contentLength
});   

Is RestRequest.AddFile not ready for .NET Core? I see there is a #if FRAMEWORK compiler directive around the AddFile methods on the interface. I think the @bcabrera workaround could work for my use case but the unit test would be a challenge.

There is no version for .NET Core just yet. See open issues to monitor progress.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomgallard picture tomgallard  ·  6Comments

ChenJasonGit picture ChenJasonGit  ·  5Comments

ghd258 picture ghd258  ·  6Comments

nilsga picture nilsga  ·  5Comments

abishekrsrikaanth picture abishekrsrikaanth  ·  3Comments