First off, thank you to all who have contributed to RestSharp. It has been extremely useful to me, and I have learned a lot by digging through it. Thank you so much.
I am currently working on an application that needs to upload files to a RESTful service via a multipart-form POST. RestSharp has several different ways to do this, but I ran into an issue when trying to report the upload progress of files (while executing the POST asynchronously, of course). The only way I could see to report progress was to pass add the file(s) using the following overload of the RestRequest.AddFile method:
request.AddFile(string name, Action<Stream> writer, string fileName, string contentType)
From the UI thread, I would pass along a writer that would report progress during the file upload. However, the POST fails every time because the content-length of the request isn't set correctly. In looking through the RestRequest class, I found that the FileParameter.ContentLength is not unless you pass along a byte[] instead of an Action<Stream>. And if I pass along a byte[] instead of a function, I can't report the progress of the upload.
My quick solution was to add the following overload to the RestRequest class:
/// <summary>
/// Adds the bytes to the Files collection with the specified file name and content type
/// </summary>
/// <param name="name">The parameter name to use in the request</param>
/// <param name="writer">A function that writes directly to the stream. Should NOT close the stream.</param>
/// <param name="fileName">The file name to use for the uploaded file</param>
/// <param name="contentType">The MIME type of the file to upload</param>
/// <param name="contentLength">The size (in bytes) of the file to upload</param>
/// <returns>This request</returns>
public IRestRequest AddFile(string name, Action<Stream> writer, string fileName, string contentType, long contentLength)
{
return AddFile(new FileParameter { Name = name, Writer = writer, FileName = fileName, ContentType = contentType, ContentLength = contentLength });
}
This provides the functionality I need. I wanted to post this to the group to see if there was a better way to do this, or if this is something that would be helpful to others.