How to use restsharp to download file How to use restsharp to download file xml xml

How to use restsharp to download file


With RestSharp, it's right there in the readme:

var client = new RestClient("http://example.com");client.DownloadData(request).SaveAs(path);

With HttpClient, it's a bit more involved. Have a look at this blog post.

Another option is Flurl.Http (disclaimer: I'm the author). It uses HttpClient under the hood and provides a fluent interface and lots of convenient helper methods, including:

await "http://example.com".DownloadFileAsync(folderPath, "foo.xml");

Get it on NuGet.


It seems SaveAs was discontinued. You can try this

var client = new RestClient("http://example.com")    byte[] response = client.DownloadData(request);File.WriteAllBytes(SAVE_PATH, response);


In case you want async version

var request = new RestRequest("/resource/5", Method.GET);var client = new RestClient("http://example.com");var response = await client.ExecuteTaskAsync(request);if (response.StatusCode != HttpStatusCode.OK)    throw new Exception($"Unable to download file");response.RawBytes.SaveAs(path);