If you use this code as mentioned in few other blogs and videos:
Map<String, String> headers = new HashMap<String,
String>();
headers.put("Content-Type",
"multipart/form-data");
byte[] fileContent = FileUtils.readFileToByteArray(new
File(filePath));
RestAssured.given().headers(headers).body(fileContent).post(url);
There are high chances that you will get errors related to content-type or "400 - Request is not a multipart request". So, the solution is to use:
.multiPart("file", new File("/path/to/file"),"application/pdf").
Please note that I have used "application/pdf" as 3rd param in the multiPart method and this value should be passed as per the file type that you are uploading like for the png file it should be "application/octet-stream", for JSON file it should be "application/JSON".
multiPart is an overloaded method that can take max 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")
Another NOTE: Rest assured doesn't allow 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"
unless we override it by providing the 3rd param.
Comments
Post a Comment