Java how to send file in request body rest assured

Java how to send file in request body rest assured

To send a file in the request body using Rest Assured, you can use the body method and pass it the file path as a string.

Here is an example of how you can do this:

refer t‮uttual:o‬ri.com
String filePath = "path/to/file.txt";

given()
    .contentType("multipart/form-data")
    .multiPart(new File(filePath))
.when()
    .post("/upload")
.then()
    .statusCode(200);

In this example, we specify the content type as "multipart/form-data" and use the multiPart method to attach the file to the request. We then send a POST request to the /upload endpoint and assert that the response has a status code of 200.

You can also use the attach method to attach multiple files to the request:

given()
    .contentType("multipart/form-data")
    .multiPart("file1", new File("path/to/file1.txt"))
    .multiPart("file2", new File("path/to/file2.txt"))
.when()
    .post("/upload")
.then()
    .statusCode(200);
Created Time:2017-11-03 23:27:11  Author:lautturi