Some APIs take files as their input to upload the file to servers. For this rest assured provides a function named multiPart(), that can be used to upload files. This function takes 3 parameters.
a) The KEY that identifies the file. Highlighted as "A" below.
b) File object. Highlighted as "B" below.
c) Content-type. Highlighted as "C" below.
.multiPart("A", new File(B), "C")
Sample with dummy values: .multiPart("file", new File(TestUtil.getFilePath("/samples/fileOne.html")), "text/html")
NOTE: Rest assured doesn't allowed to use .body() and .multiPart() at the same time within it's given() function. And when we use .multiPart(), the content-type automatically get set to "multipart/form-data".
restassured automationtesting json api multipart fileupload
1) Using Java (Lengthy way) : Create a utility and use it:>> import java.io.BufferedOutputStream; import org.openqa.selenium.io.Zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipUtil { private static final int BUFFER_SIZE = 4096; public void unzip (String zipFilePath, String destDirectory) throws IOException { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); // to iterates over entries in the zip folder while (en...
Comments
Post a Comment