There could be a service that takes a file as an input and upload it to the server or cloud storage etc. It could be any kind of file like text data, image, etc. Let’s take this simple scenario, you’ve might be using some social media applications like Facebook, Instagram, etc. And, you might upload your images on those applications. Let’s assume there is a service behind it that handles the image upload.  Now, Rest Assured has the capability to test that. In this example, we are going to see How to upload a multi-part file and send it with the HTTP POST request using Rest Assured?”

Rest Assured has provided us this capability through ‘multiPart()’ method which could be used to upload the files. 

NOTE:

In order to proceed further to look for the Rest Assured solution, first we need some running service which can be used to test it. Well, nothing to worry, I’ve made it easier for you. I wrote a sample Rest Service using Spring boot which takes a file as an input. You don’t need to waste your time surfing on the internet to look for any online service which you could use to test this Rest Assured feature.

What you just need to do is to download this project from GitHub by clicking on this link and import it in your IDE and just run it.

OR

You can have your own running service that you could use to test this feature.

Simple representation of uploading a file using RESTAssured...!!! Share on X

Check out: How to send GET, POST, PUT, PATCH and DELETE Requests using REST-assured

Here are some details about the service that we are going to test by uploading the file:

  • Endpoint: http://localhost:8080/uploadFile
  • Request Type: POST
  • File to upload: any file (here we are uploading .png)

Objective:

At the above service endpoint URL, we are going to Upload a file and validate its response.

Finally, here is the code to upload a multipart file through the POST request to the above-mentioned REST API Service Endpoint:

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “localhost:8080” as the root URL of the service.

2. Specifying the exact resource to look for and make a multipart file upload with the POST request to that resource

Here, using this code, we are looking to upload a test file using the multiPart method with the POST call to the exact resource which is “/uploadFile” in this case. Hence, the complete Service Endpoint would be “http://localhost:8080/uploadFile” to which we are sending a POST request with the multiPart file upload. 

3. Response validation

a. response.getStatusCode() : This line of code would extract the status code from the response.

b. response.asString() : It displays the response in a string format

c. assertTrue(response.asString().contains(“successfully uploaded”)) : This line of code will throw true or false based on the condition if the string ‘successfully uploaded‘ present in the response or not.

Let’s have a look at the Eclipse Console Output:

That’s it, it’s that simple to upload a file using REST Assured API: ?

Simple representation of uploading a file using RESTAssured...!!! Share on X

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak