How to use HTTP Request to Send Multiple JSON Files?

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.

SHARE THIS

Get A Free Quote

2 thoughts on “How to use HTTP Request to Send Multiple JSON Files?

  1. It worked with me … however when you try to add variables like “${test}” in the body to get the values from another variable it didn’t work ?
    do you know a solution for that ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.