Skip to content

How to get response content without storing it in MemoryStream #1794

Description

@Racer396

Hello!
Before v107 i use DownloadData with AdvancedResponseWriter, and as i remember, it doesn't store ResponseContent in memory and allow to write something like this:

               var request = new RestRequest(downloadUri, Method.GET);
                request.AdvancedResponseWriter = (stream, response) =>
                {
                    using var fStream = File.OpenWrite(filePath);
                    var buffer = new byte[1024*64];
                    long totalCount = 0;
                    while (totalCount != response.ContentLength)
                    {
                        var count = stream.Read(buffer, 0, buffer.Length);
                        totalCount += count;
                        fStream.Write(buffer, 0, count);
                    }
                };
                _client.DownloadData(request);

Now in v107.3, when i use DownloadDataAsync or just ExecuteAsync, it store all data in memory and only after that return me the MemoryStream.
How can i avoid storing ResponseContent in memory and write it in the file as is?
Because in my case, i need to downloading big files, which size can be a few gigabytes, and storing it in memory, on clients desktop PC - looks like a bad idea.

public static Task<Stream?> ReadResponse(this HttpResponseMessage response, CancellationToken cancellationToken) {

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions