Skip to content
Software Testing Journal
Software Testing Journal

Your daily dose of learning!

  • Blog
  • Help Center
  • Video Playlist
  • Podcast & FAQ
  • Privacy Policy
Software Testing Journal

Your daily dose of learning!

Post using Multipart: How to upload a file using REST Assured?

Dheeraj Gambhir, 2023-09-112023-09-11

If you use this code as mentioned in a 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 a 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 for the PNG file it should be “application/octet-stream”, for the 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 its given() function. And when we use .multiPart(), the content-type automatically gets set to “multipart/form-data” unless we override it by providing the 3rd param.

If you find this post helpful, please share them with your colleagues and friends.

  • Facebook
  • Twitter
  • LinkedIn
  • Email
Automation APIRestAssuredTips

Post navigation

Previous post
Next post
©2025 Software Testing Journal | WordPress Theme by SuperbThemes