Posted in Automation Testing

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");

2 thoughts on “Rest Assured: Multipart file upload

  1. Nice and clean explaination, but lacks example for latest rfc7578 forms, like:
    ——WebKitFormBoundaryGAnAFaOOKFFAFJyc
    Content-Disposition: form-data; name=”formField1″
    formField1Value

    ——WebKitFormBoundaryGAnAFaOOKFFAFJyc

    As you can see – it has no content type, no file, just pure data

    Like

    1. Thanks for your Feedback. Good point, I will surely add such examples and few more soon.
      I Appreciate it your effort in sharing feedback.

      Like

Leave a comment