Restsharp: Multipart uploading - wrong content length

Created on 28 Mar 2017  路  3Comments  路  Source: restsharp/RestSharp

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:
https://github.com/restsharp/RestSharp/issues/742

Best regards,
Alex

Most helpful comment

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
                });

All 3 comments

Hi Alex,

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

Best regards,
Maurice

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
                });

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nilesh-shah picture nilesh-shah  路  6Comments

wojciechrak picture wojciechrak  路  3Comments

Taher-Assad picture Taher-Assad  路  5Comments

instriker picture instriker  路  7Comments

weswitt picture weswitt  路  3Comments