Testing REST API File Uploads in JMeter

DZone 's Guide to

Testing REST API File Uploads in JMeter

Learn how to create a multipart POST or PUT request from scratch, and see a demo to create and update a file in Google Drive using both POST and PUT requests from JMeter.

· Integration Zone ·
Free Resource

Creating a Multipart POST or PUT Request From Scratch

Simulating a file upload in a JMeter test is fairly straightforward and easy. This blog has covered the topic in Performance Testing: Upload and Download Scenarios with Apache JMeter

Given that you provide a valid path to a file (either full or relative) and check the Use multipart/form-data for HTTP POST box in the HTTP Request sampler, it shouldn’t be a problem to upload a file to the server and collect the relevant performance metrics. 

However, the above approach does not cover every possible scenario. When it comes to passing additional parameters along with the file upload request, or using an HTTP method other than POST (i.e. PUT or PATCH), the usual instructions for uploading files with JMeter might not be applicable. In certain scenarios, for example, if you need to test a file upload via some REST API endpoint, you will need to build an HTTP request manually.

This article will explain how you can create a multipart POST or PUT request from scratch.

NOTE: All examples below assume using Google Drive REST API to upload new and update existing files in Google Drive.

Uploading a REST API File Via the POST Method

HTTP Request Sampler: Endpoint

First, you will need a test element which will be doing the main part of the work. You’re probably going to choose the most frequently used one - the HTTP Request Sampler. In order to upload a file to Google Drive it needs to be configured as follows:

  • Server Name or IP: www.googleapis.com
  • Protocol: https
  • Method: POST
  • Path: /upload/drive/v2/files?uploadType=multipart


Testing Advanced File Uploads in JMeter


It’s important to note that the “Use multipart/form-data for POST” box is not checked.

HTTP Header Manager: Authorization and Content Type

Second, you need to provide some authentication information so that Google Drive can recognize your account and add the document to it. Google Drive uses OAuth2, so you need to provide a valid OAuth token along with the request. There are several ways of obtaining the token, and you might want to check out our How to Run Performance Tests on OAuth Secured Apps with JMeter guide for comprehensive information on bypassing OAuth challenges in JMeter tests.

"Authorization" Header

The first header you need to send along with the request is the Authorization header. In the case of Google Drive, it assumes through OAuth2 that the header should look as:

  • Name: Authorization
  • Value: Bearer ${your_oauth_token}

"Content-Type" Header

As we’re not using the “multipart/form-data” for POST, we need to add the relevant header manually. Google Drive REST API requires the Multipart/Related content type, and the request should consist of 2 parts:

  1. Metadata part - usual JSON where you can specify elements, such as destination file name
  2. Content part - actual contents of the file being uploaded

The parts should be split by a multipart boundary, a free form separator which acts like an "&" character in non-multipart requests parameters, so that the server is aware where one part of the multipart request ends and the next part begins.

Assuming all of above, the “Content-Type” header should have:

  1. a multipart/related line
  2. any boundary which will be used to separate parameters in the request body

An example configuration can be look something like:

  • Name: Content-Type
  • Value: multipart/related; boundary=jmeter_is_great

To send these 2 headers with your HTTP Request, add an HTTP Header Manager to your Test Plan and configure it so that it looks similar to the below image (you need your own OAuth token and may change the boundary to whatever you like. A good practice is having it be randomly generated via the __RandomString() function.


Advanced File Upload Testing in JMeter HTTP Header


HTTP Request: Building the Request Body

The file upload request body for Google Drive should look like:

Content-Type: application/json; charset=UTF-8

JSON Metadata
Content-Type: file MIME type

File content

For example, given the multipart boundary jmeter_is_great specified in the HTTP Header Manager, the HTTP Request sampler body should look something like:

JMeter Advanced File Upload Testing


The above configuration assumes the plain text “Hello from JMeter” file inlined directly in the request body. If you want to upload an existing file, you can use the __FileToString() function.

Let’s run the request and see how it goes:

HTTP Request: Building the Request Body


As you can see from the View Results Tree listener, the request is successful and the Google Drive API endpoint responds with the uploaded file’s metadata in JSON format.

REST API: Updating a File via PUT or PATCH

Updating a file via a REST call is similar to uploading one using the POST request. All you need to do is:

  1. Choose and appropriate the “Method” using the dropdown menu in the HTTP Request Sampler.
  2. Change the endpoint URL if required.
  3. Amend the request body according to your needs.

Regarding point number 2, in the case of Google Drive API Update method, you need to substitute the ?uploadType=multipart URL part with the previously uploaded file ID which can be extracted via either the JSON Path PostProcessor or the JSON Path Extractor

The HTTP Request sampler for updating the file via the PUT request will look like: 

REST API - Updating a File Via PUT in JMeter



Finally, it’s time for a demonstration of creating and updating a file in Google Drive using both POST and PUT requests originating from JMeter. 

On the left side, you can see JMeter executing the file upload and file update requests, and on the right side, there is a Google Drive application opened in a browser where the file appears and is being updated.

creating and updating a file in Google Drive using both POST and PUT requests originating from JMeter


Hopefully, now you know how to manually build a multipart upload request and how to issue both PUT and PATCH requests. If the HTTP Request sampler still doesn’t provide enough flexibility to accomplish your test scenario, you can consider using HTTP Raw Request, available via the JMeter Plugins project. It provides total control of headers, cookies, content, etc. However, in 99% of use cases, JMeter’s HTTP Request sampler should be enough.

If anything remains unclear, feel free to use the comments form below to ask your question or share any form of feedback.

google drive, jmeter, rest api

Published at DZone with permission of Dmitri Tikhanski , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}