12

I want to send below as a form-data in API Body for a PUT request:

  1. Upload a file(KEY) with "Error.png"(VALUE)
  2. Send text, "MyName"(KEY) with false(VALUE)

How to do this using REST-Assured

Attached is the screenshot enter image description here

Damini Suthar
  • 1,470
  • 2
  • 14
  • 43
D Bhatnagar
  • 133
  • 1
  • 1
  • 9

4 Answers4

14

You need to set desired content type i.e "multipart/form-data" and add the multipart request specs to the request. Eg.

        given()
            .contentType("multipart/form-data")
            .multiPart("file", "filename")
            .multiPart("key", "value")
            .when()
            .put(endpoint);
rohit.jaryal
  • 320
  • 1
  • 8
5

Be sure to include a file object if you're doing a file upload. It should look like this:

given()
    .contentType("multipart/form-data")
    .multiPart("id", "123")
    .multiPart("file", new File("./src/test/resources/test-file.txt"))
    .post("api/endpoint")
.then()
    ...
Evan Siroky
  • 9,040
  • 6
  • 54
  • 73
1

Most of the times we need to convert image into readFileToByteArray.

    String file = "/Users/Downloads/file.png";

    byte[] fileContent = FileUtils.readFileToByteArray(new File(file));

    RestAssured.baseURI = "Enter Base uri";

    Response res = given()

            .header("Accept", "application/json")
            .header("Content-type", "multipart/form-data")
            .formParam("token", "08bc73deff88dd3d44bb1bf65b55d4ff")
            .multiPart("asset", "image/png", fileContent).when()
            .post("api/endpoint");  

    System.out.println(res.getStatusCode());

    System.out.println(res.jsonPath().prettify());  

Please make sure the image mime type (specified in the example as "image/png").

Sanath
  • 4,774
  • 10
  • 51
  • 81
0
 // uploading image on click of upload button
//fileNames is state
const [fileNames,SetfileNames] = useState("");
  const updatecsvfile = (e) => {
    e.preventDefault();
    const formdata = new FormData();
    formdata.append("document", fileNames);
    let verify = localStorage.getItem("_id");
    console.log(verify);
    console.log("Data", formdata);

    // posting to api by formdata
    axios
      .post(`${SERVER_URL}/uploadDocument?_id=${docids}`, formdata)
      .then((res) => {
        console.log("res", res.data.status);
        let Status = res.data.status;
        if (Status === "success") {
          window.$("#uploadproject_doc").modal("hide");
          window.$("#doc_added").modal("show");
          window.setTimeout(function () {
            window.location.reload();
          }, 2000);
        } else {
          alert("Details are already Exists");
        }
      });
  };
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '22 at 19:31