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

Multipart uploading - wrong content length #939

Closed
AlexSchoenfeld opened this issue Mar 28, 2017 · 3 comments
Closed

Multipart uploading - wrong content length #939

AlexSchoenfeld opened this issue Mar 28, 2017 · 3 comments

Comments

@AlexSchoenfeld
Copy link

Hi,

I'm trying to get multipart uploading working with v105.2.3, but it seems there is something wrong with the Content-Length.

using (FileStream source = File.Open(path, FileMode.Open))
{
    RestSharp.RestRequest request2 = new RestSharp.RestRequest();
    request.Resource = $"/files/uploads/{UploadId}";
    request.Method = Method.POST;
    request.AddFile(filename, source.CopyTo, filename);
    request.AlwaysMultipartFormData = true;

    IRestResponse<UploadResponse> response2 = this.Client.Execute<UploadResponse>(request);
    if ((int)response2.StatusCode != 201)
    {
        throw new Exception(response2.StatusDescription);
    }
}

The response2 has this Error Message (it's in german and means Request was cancelled.):
Die Anfrage wurde abgebrochen: Die Anfrage wurde abgebrochen..

Inner Exception (Cannot close the stream until all bytes are written):
Stream kann nicht geschlossen werden, bevor alle Bytes geschrieben wurden.

In Fiddler I get this:

Request - Entity

Content-Length: 210
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299

Response

HTTP/1.1 408 Request body incomplete
The request body did not contain the specified number of bytes. Got 159, expected 210

I need to use the Nuget package with this versions since I have other packages (like RestSharp.Newtonsoft.Json) that require this version.
Seems like the same problem here:
#742

Best regards,
Alex

@mdallau
Copy link

mdallau commented May 5, 2017

Hi Alex,

I have the same issue. Have you solved it or worked arround it?

Best regards,
Maurice

@theclunkymonkey
Copy link

theclunkymonkey commented Aug 22, 2017

I had this same issue and resolved it by using request.Files.Add() method instead of AddFile() like so:

                request.Files.Add(new FileParameter
                {
                    Name = "fileAttach",
                    Writer = (s) =>
                    {
                        FileStream stream = File.Open(_attachment.path, FileMode.Open);
                        stream.CopyTo(s);
                        stream.Dispose();
                    },
                    FileName = _attachment.name,
                    ContentType = _attachment.contentType,
                    ContentLength = _attachment.size
                });

@alexeyzimarev
Copy link
Member

You can use AddFile but you need to specify the content length, there is a parameter for that.

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

No branches or pull requests

4 participants