In this JMeter Tutorial, we explain how to send one or multiple JSON files in the body of the HTTP request.
When testing a RESTful API, we normally send a POST request to the REST API in JSON format. The request parameters in JSON format needs to be sent in the body of the HTTP request.
First, you will need a text file with the JSON request. We will use this file to send to our REST API in the body of the POST request.
Suppose, the file is located in /Users/testing-excellence/Perf/blog/json_request.txt
Our JMeter script will look something like this:
In other words, we need to select the Body Data in the HTTP Sampler and insert the following line of code
Obviously, the path will be different on your machine.
The above line takes the contents of the file named json_file.txt and sends the contents in the body of the request to the REST API.
As JMeter is mainly used for performance and load testing, we can load test our REST API by sending multiple JSON requests.
To do that we make use of the JMeter’s CVS Data Set Config element.
In the CSV file, we have a column with the names of the JSON request files. This CSV file serves as a reference to the JSON files.
Suppose we have 10 JSON files, with filenames 100.txt, 101.txt, 103.txt….110.txt
And the CSV file will look like:
Then in JMeter, we add the CSV Data Set Config element to our test plan
The variable is JSON_FILE which takes the values of 100, 101, 102, etc… from the CSV file.
Then we need to reference this variable in our __FileToString() JMeter function, i.e.
When we execute this JMeter test, it will loop through the CVS, taking each file name and extract the contents of each file to send as a JSON in the body of the request.