8

I'm using RestSharp in a Mono project to upload some files and I have noticed that when a large file is uploaded, the memory grows substantially.

Looking at RestSharp source code I did notice that FileParameter expects a byte array, which means it is really loading the file into memory.

Am I doing something wrong? Is there a way for RestSharp not do this? I might be uploading really large files so, uploading them from memory is not an option.

Any help (including telling me to use another HTTP library available on mono) is welcome.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158

2 Answers2

5

Use the AddFile(name, writer, filename) overload.

For the writer parameter, pass an Action<Stream> that writes directly to the request body stream. Do not close the stream.

Here's an example for writing to the stream.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 4
    There is no way out, the body is always loaded into memory -> https://github.com/restsharp/RestSharp/blob/master/RestSharp/RestClient.cs#L433 – Maurício Linhares Jan 25 '12 at 15:27
  • As of 2015, it looks like it's [properly streaming the body](https://github.com/restsharp/RestSharp/blob/62335a677c92ad45d9dc3214ff6765b597d8642f/RestSharp/Http.Sync.cs#L226), specifically in [`WriteMultipartFormData`](https://github.com/restsharp/RestSharp/blob/bf9b79614d7fc3468d2a830e2e65977a0c3246c2/RestSharp/Http.cs#L390) – drzaus Jul 14 '15 at 17:58
  • I tried to fix the broken link, but I think they changed the API so much, the call you are talking about is not existing anymore, it now wrap the stream into an `AddFile(name, path)` so you don't need to provide it anymore. Not sure if you could adjust your answer since it has been so long ago.... – ForceMagic May 21 '20 at 20:04
5

And I gave up after I found this line, so request bodies are always loaded into memory, which is unfortunate, so I built a simple solution to do file uploads based on code from this question and from debugging the Apache HttpClient library.

In case someone is interested, the source is available here.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • I think the code shifted. Could you update the link to point to a specific commit + line no? – Gary Johnson Apr 24 '15 at 11:29
  • 1
    As of 2015, it looks like it's [properly streaming the body](https://github.com/restsharp/RestSharp/blob/62335a677c92ad45d9dc3214ff6765b597d8642f/RestSharp/Http.Sync.cs#L226), specifically in [`WriteMultipartFormData`](https://github.com/restsharp/RestSharp/blob/bf9b79614d7fc3468d2a830e2e65977a0c3246c2/RestSharp/Http.cs#L390) – drzaus Jul 14 '15 at 17:57
  • Same as for the rest of the thread, I tried to fix the link, but if you could double check that your answer is still valid, I am not sure. – ForceMagic May 21 '20 at 20:06