Your JMeter test will look like this (I did not include the listener).

Now if you run this, you should be able to send the request and get the response successfully.

Note: If it does not work for some reason, try changing the HTTP Request Implementation to Java/Other values and give a try.

Whatever we have done so far is a very simple test – like a POC. Now lets see how it can be improved further.

Parameterizing the test data:

In the above example, we send the hard coded request JSON to test the API. We might have to test the API for different sets of data.

Lets take a very simple example, I have an API which gives the book name and price details for the book id request i send.

Testdata in CSV:

Request JSON:

Lets assume that our API request in the below format.

{
"bookid": 1
}

Response JSON:

Lets assume our API response is as given below.

{
"bookname": "Awesome Jmeter"
"bookprice": 23.5
}

Add CSV Data Set Config under Test Plan & update the details as given above.

Variable Names : can be blank. JMeter will take the CSV column names as variable names.

Recycle on EOF? : should be FALSE. We are going to test each row only once.

Stop Thread on EOF? : should be TRUE. It will stop the JMeter test automatically once we tested for all the rows.

In Thread Group, select the ‘For Ever’ check box in the Loop Count.

Parameterize:

Instead of sending the hard coded book id, lets parameterize it by using the value from the ‘id’ column of the CSV file.

That’s it. If you run the test now, it should send 6 requests one by one for each row in the CSV file.

We can do a lot better than this – what if there are different types of request – GET / POST / DELETE / PUT / PATCH etc. It is super easy with JMeter. I drive my tests completely through a spreadsheet as shown here. Please check here for the advanced usage of JMeter – REST API – Data-Driven testing

Assertion:

If we can send the request and get the response, are we done? Nope!! But, We are almost there!!

We need to validate if we get the expected response. So, one last step would be to add the assertion.

I have the expected book name and price details in the CSV file shown in the above example. Lets use those details to check if our API works as expected.

Add Response Assertionunder HTTP Request as given below. We need to verify if we have the expected book name and price in the response. Add them as given below.

This assertion will validate the response and Pass/Fail the test accordingly. (Ensure that Pattern Matching Rules is ‘Contains’ as we are checking only for the book name and price. We are not verifying the whole response.)

Duration Assertion:

JMeter not only verifies text – but also the ‘time‘ the request took. If we add ‘Duration Assertion’ and set an acceptable time limit, JMeter can confirm if the response was received within the time limit.

Extracting Data From JSON Response:

Sometimes you might want to extract specific data from JSON response and pass this data to the subsequent request in JMeter. JMeter provides a JSON Extractor to extract particular value from the JSON response.

JMeter can help here as well by using the same test we had just created!!

Update the Number of thread in the Thread Group. If you enter 10 as number of users & run, JMeter will simulate 10 concurrent users.

Update the Ramp up period (in seconds). For ex: If you have 10 users & 50 seconds as Ramp up period, JMeter will start creating 1 virtual user for every 5 seconds.

Loop count: Select ‘Forever’.

Under Scheduler, enter Duration in seconds. [Entering 600 will run the performance test for 10 mins with 10 Virtual Users.]

Update CSV Data Set Config : ‘Recycle on EOF?’ – set it to TRUE – [FALSE will read the CSV data only once. We need enough data for VUsers to run for 10 mins. So setting it to TRUE will feed the same set of test data once it is read]

Add a ‘Aggregate Report’ listener under the Test Plan.

Run your Meter test now. ‘Aggregate Report’ will give a basic idea of your REST API performance metrics like number of requests made in given duration, average response time, throughput, ‘90% line’ etc. [Note: It is not a good practice to run JMeter performance test in GUI mode as we just did now as it consumes more memory. Please check this Tips & Tricksfor the best practices to be followed.]

Summary:

We just have created a Simple Data Driven Framework to test the functional behavior of REST API using JMeter without using any programming language. We also saw how to do performance testing on our API using JMeter by reusing the same script created for functional testing.

Hi,
I may not be able to provide the actual request but will provide the details of my request
HTTP Request Sampler:
Host name: ; Port: 8080
Implementation Java; Protocol: HTTP; Method POST; Content encoding; UTF-8
Path: /rest/user/register// user id is any random number and access token is passed as NULL so that the system identifies it as a System Admin user registering to the service
Follow redirects: Checked; Use Redirects: Checked
Raw POST Body : {“name”:”adminNew”,”userCategory”:”SYSTEM_ADMIN”,”companyCode”:”DEFAULT_COMPANY_CODE”}
Http Header Manager
Content-Type: application/json

I was just wondering whether this could be something related to Json lite not recognized by the server. Kindly put in your thoughts for the same.

I am resending the requests in my previous comment which has not appeared
Path: /rest/user/register/userid/accesstoken where user id is any random number and access token is passed as NULL so that the system identifies it as a System Admin user registering to the service

As you confirmed that you 415 error even with Chrome extn, the issue does not seem to be specific to JMeter.

Your setup looks good to me. Lets try these!

Add ‘Accept = application/json’ in the header manager.

You seem to have set UTF-8 already. Try removing this.

If it is still does not help, Can you see how the request is being sent by the Postman client – using JMeter proxy server? If we can record, then we will know what is missing in the request we send through JMeter.

Hi,
My script started to work as the dev team confirmed that they are serializing the json to string at client side and desterilize it at server. So i gave text/plain as content-tpe and it started working. Thank you for all the help in understanding things.
I have a new problem, I am using Response Assertion to validate my response against csv. But I would also like to enter the status of each request in the csv against each row. Will I be able to use Bean Shell assertion here if so how? Kindly explain

Thank you for the response, this works. I tried adding a BeanShell listner to the plan which actually writes the error code from the response. But the write file loops infinitly, i.e the values are entered endlessly in the csv. Below is my code

When we use the CSV file for Post Request than we need to change the content-type from application/json to text/csv ? if yes then if you services only allow json request then you can advise the solution?

Aim of this article is to test a REST API usinh JMeter. For that the content-type should be application/json only.
Uploading some CSV files using HTTP requests ca also be done using JMeter. You can simply record your actions on Browser using JMeter.

I using protocol https with 3 headers to test API. My test succeed in post man but i have response code 401 ( unauthorized ) when setting up and running in jmeter. I’m using same value for header parameter in postman and jmeter.
Please help me! Thanks so much!

I was trying to make a script to test REST services using Jmeter.
Till now I was using Chrome’s Advanced REST Client.
My authentication request was GET and it was something like this in Advanced REST:https://username:password@URL:portnumber
its a GET request

Now when I am using Jmeter. I tried following ways:
I added HTTP Authorization Manager and mentioned Base URL and Username/password inside it.
When I am trying to do a request then its showing me “Unauthorized”

I also tried to login using normal https request but no success.

When accessed manually, a authorization popup window appears and username and password is submitted inside this window.