Rest Assured: Multipart file upload

When sending larger amount of data to the server it’s common to use the multipart form data technique. Rest Assured provide methods called multiPart that allows you to specify a file, byte-array, input stream or text to upload. In its simplest form you can upload a file like this:

Option 1: File (.json , .txt) without control name (Default control name: file)

given().
        multiPart(new File("/path/to/file")).
when().
        post("/upload");

Option 2: File (.json , .txt) with Control name

given().
        multiPart("controlName", new File("/path/to/file")).
when().
        post("/upload");

Option 3: File (.pdf) with Control name

given().
      multiPart("controlName", new File("/path/to/file"),"application/pdf").
when().
        post("/upload");

Option 4 : json object with Control name

given().
        multiPart("controlName", jsonObject,"application/json").
when().
        post("/upload");

Option 5 : PDF file + json object with Control name

given().
      multiPart("controlName", new File("/path/to/file"),"application/pdf").
      multiPart("controlName", jsonObject,"application/json").
when().
        post("/upload");

Leave a Reply