Usually, we use the JSON format to send a POST request while testing a REST API. It uses the HTTP body to send the request parameter in JSON format.

The first and the foremost, you will require a JSON request in a text file. This file can be sent in the body of a POST request.

Consider the following file location: /Users/XYZ/Perf/blog/json_request.txt

Consider the following JMeter script:

json_request.txt

{“menu”:{

“id”:”file”,

“value”:”File”,

“popup”:{

“menuitem”:[

{“value”:”New”,”onclick”:”CreateNewDoc()”}

{“value”:”Open”,”onclick”:”OpenDoc()”},

{“value”:”Close”,”onclick”:”CloseDoc()”},

]

}

}}4

Then, you need to add the following line of code in the selected body data of HTTP Sampler.

${__FileToString(/Users/XYZ/Perf/blog/${__eval({json_file})

}.txt,,)}

Note: The path might change from different machines.

How to use HTTP Request to Send Multiple JSON Files

We can use JMeter to send multiple JSON request and load test our REST API. You need to make use of CSV Data Set Config element to do this. The column with the name of JSON request files cater the need to refer JSON files.

Suppose we have 11 JSON files: 100.txt, 101.txt, 102.txt, and so on.

So, the CSV file we look like:

CSV file

Then, using JMeter we need to add the file to our test plan, as shown below

test plan

The JSON_FILE variables will take the values from CSV file. Then, we need to use the __FileToString() JMeter function to reference the variable.

Finally, when you will execute the test, it will extract the content from each file one by one and send as JSON in the request body.