How to send multiple json body using jmeter? - rest

I have written a REST API and now my requirement is to send the multiple JSON body to the API using POST method from JMeter. I have a csv file with four values(1,2,3,4). And in each of the four files I have the JSON body. I used :
Step-1) added the csv file to jmeter and create a reference and named it JSON_FILE
Step-2) ${__FileToString(C:Path_to_csv_file/${__eval(${JSON_FILE})}.txt,,)}
But from this I am able to access only first file i.e which is named with one. How do I send the body of all file to the API?
Help is highly appreciated.

You won't be able to use CSV Data Set Config as it will read the next value for each thread (virtual user) and/or Thread Group iteration.
If your requirement is to send all the files bodies at once you can go for an alternative approach
Add JSR223 PreProcessor as a child of the HTTP Request sampler which you use for sending the JSON payload
Put the following code into "Script" area:
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
the above code iterates through entries in plans.csv file, reads the file contents into a string and concatenates them altogether. Once done it sets the HTTP Request sampler body data to the generated cumulative string.
Check out The Groovy Templates Cheat Sheet for JMeter to learn more and what else could be achieved using Groovy scripting in JMeter.

Use Body data as follows in HTTP Sampler:
{__FileToString(${JSON_FILE},,)}
You have to put all the file path in your plan.csv file. At each line, there should be a file path.
Example:
Suppose, you have 4 files with JSON body which you want to use in your HTTP sampler.
Give the file path of these 4 files in your CSV file which is plan.csv. Each line contains a file path like this:
/User/file/file1.json
/User/file/file2.json
/User/file/file3.json
/User/file/file4.json
Now, in your CSV data set config, Use the proper file name of CSV file which contains all the file path and give it a variable name like JSON_FILE.
Now, Use {__FileToString(${JSON_FILE},,)} this line in your Body data. Also use the loop count value accordingly.

Related

Invalid entity passed while trying below method [duplicate]

I am using Karate 0.9.0 version and I want to upload data using csv file. As per new update, it is converting data into JSON. But my API supports csv file format for upload function. How can I upload csv file in post request without converting data into json?
Example
Given path 'xxx/upload'
And header Authorization = xxx
And header Content-Type = 'text/csv'
And request read('classpath:xxx.csv')
When method POST
Then status 202
P.S. This example was working in Karate version: 0.9.0.RC5
Thanks, that is indeed an edge case we hadn't thought of, but you have 2 options that will work nicely:
1) rename your CSV file to *.txt
And request read('classpath:xxx.txt')
2) use the karate.readAsString() API
And request karate.readAsString('classpath:xxx.csv')

How to read actual file contents by name in CSV Data Set config - Jmeter

I was writing JMeter tests for REST API.
Its a post request and we need to send a big xml content in request body.
So I was using CSV Data Set config to parameterize the xml content in body part.
I have created a CSV Data Set config for HTTP Request sampler.
In csv file, I am writing the whole xml content. 1 row for 1 request. It is working fine.
But I found this is bit complex as we have to maintain large lines of xml in csv file.
Is there any way we can write only xml file names or full paths in csv file and CSV DataSet config checks the name and then read the contents of that file and append in request body.
file-abc.xml
file-def.xml
I think this would be easy to maintain as we can have dedicated files for XML content.
Any way to do it using CSV DataSet config?
Or any other way to achieve the same in JMeter tests.
I found this question How to hold Xml file names in CSV Data set Config (Jmeter)
I followed its answer but I am not able to pass the xml content in request body.
Its only passing xmlfile names written in csv file in the request body.
But as per answer it reads the file from xml path/name and pass it in the parameter.
You can keep the file names or paths to the files in the CSV file and read the file content using __FileToString() function directly in the HTTP Request sampler body
If you're keeping XML files in a separate folder you might find Directory Listing Config plugin easier to use in case you want to add/remove/rename files without having to maintain the CSV mapping.
Directory Listing Config plugin can be installed using JMeter Plugins Manager

How to make the search parameters in http request as dynamic in jmeter

http request: http://ipAddress:Port/SomeResource?Param1=value1&Param2=value2&......
so on.
This is a http request sample in jmeter that hits a rest api and gets response in JSON format.
Here the challenge is Param1, param2, param3 .... these search parameters number is not constant it may change depending on the call so i am making a csv file that contains rows that contain search parameters in coma separated format.
CSV file is like
param1,param2
param1,param2,param3
param1
I am using a CSV data configure to pull data from the csv file and put it in the http request
And putting this in http request like
Now if the param is null i don't want see this in http request header
so how to do this in jmeter.
Remove all "Parameters" from the HTTP Request, it should be clean
Add JSR223 PreProcessor as a child of the HTTP Request sampler you would like to parameterize
Put the following code into "Script" area:
1.upto(4, {
if (vars.get('param' + "$it") != null) {
sampler.addArgument(vars.get('param' + "$it"),'someValue')
}
})
Add JSR223 PostProcessor as a child of the request you would like to parameterize
Put the following code into "Script" area:
1.upto(4, {
vars.remove("param" + "$it")
})
That's it, you should now get what you need. You will not see the changes in JMeter GUI, you will only be able to observe them in the runtime using View Results Tree listener

How to Send multiple request concurrently/Sequentially in postman with different set of values for each request?

For example, below is the JSON request data to "add a device" in the DB.
For example, I want to add 10000 devices with different IMEI number and different phone number to the server for testing purpose. So, how to send the request at once.
I'm ready to create 10000 devices data with different values manually.
Now I can able to send one by one only.But how to send all the request at once?
{
"device_name":"34793812453274392",
"imei_num":"36xxxxxxxxxxxx5",
"phone_num":"8666606451",
"device_city":"Chennai",
"device_state":"Tamil Nadu",
}
As I'm new to POSTMAN, required detailed info. Thanks in advance.
The thing that should work is :
you prepare your input JSon body with variables. ie, from your example :
{ "device_name":{{device_name}}, "imei_num":{{imei_num}}, "phone_num":{{phone_num}}, "device_city":{{device_city}}, "device_state":{{device_state}}, }
the {{}} is for variables
You create a CSV file with the corresponding headers (one for each variable of your input JSON) and all the values you need:
example:
line 1 : device_name, imei_num, phone_num, device_city, device_state
line 2 : "34793812453274392", "36xxxxxxxxxxxx5", "8666606451", "Chennai", "Tamil Nadu"
... and so on ...
line 10000 : ...
Then, in the Postman runner (see here ), you select the data file (Data / Select file) with CSV type (you should have an option to check the content, but be careful as you'll have a lots of rows, it may take a long time, I suggest you try first with a small CSV file)
You just set ONE iteration (otherwise you'll play x times 10000 requests).
It will parse your file and, for each data line, it will send your request with replacing the body's variables by the corresponding data associated to the corresponding header. Header names must have the same label as your variables.
Launching the runner will launch your 10000 requests sequentially
If you prefer, you can use JSON input file as data file, see here
Don't hesitate to have a look at postman documentation, it's pretty complete.
There is an option called Runner at the top left corner of your Postman application. You can select the collection you need to run with number of iterations and delay time between each request. But the thing is you cannot alter the values inside the JSON request. Thanks
Put all the data into a JSON Array
and then do them all as one post. Currently you only have one set of data you're posting in.
Just create a json body with all the data you need to enter and post them into the same API endpoint.

Using variables in SOAP request file in JMeter

In a JMeter (v2.13) test plan I have a SOAP/XML-RPC sampler. The SOAP request itself is loaded from a random file.
Sample request
<mySoapRequest>
<value>555</value>
</mySoapRequest>
This works fine.
I would now like to replace this fixed value with a variable which is defined in JMeter, i.e.
<mySoapRequest>
<value>${someValue}</value>
</mySoapRequest>
It seems as if JMeter does not resolve this variable. The actual SOAP request sent to the service does not contain 555 but ${someValue}. Is there any workaround so that I could use variables in the file?
That can be done using FileToString and eval functions.
For this XML,
<mySoapRequest>
<value>${someValue}</value>
</mySoapRequest>
In the SOAP/XML RPC request Data section, use the functions as shown below to get the value replaced at run time.
${__eval(${__FileToString(C:\users\me\desktop\soap.xml)})}
__FileToString - The FileToString function can be used to read an entire file. Each time it is called it reads the entire file.
__eval - The eval function returns the result of evaluating a string expression.