Friday, March 11, 2022

How to download file's using RestSharp , WebClient & RestAssured

 



Some times we need to perform file download operations using an api , in this blog we are going to deal different methods to download a file using 

1. WebClient

2. RestSharp

3.RestAssured


Code Sample for download operation using WebClient

using (WebClient client = new WebClient())
{
     #Give the API URI and path , file need to store
    client.DownloadFile("URI Path","FileDestinationPath");
}


Code Sample for download operation using RestSharp

#Initialize the restclient
var client = new RestClient("URI Path");

#save the file to the destination path
client.DownloadData(request).SaveAs(filePath);


Code Sample for download operation using RestAssured

@Test
	Public void fileDownloadTest() {

                #Extracting response into a byteArray
		byte[] dowloadedFile = RestAssured.given().when()
			.get("URI Path")
			.then().extract().asByteArray();

		try {
			FileOutputStream os = new FileOutputStream(new File("destinationPath"));
			os.write(dowloadedFile);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
}