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 restclientvar 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(); }
}
No comments:
Post a Comment