JMeter Tutorial -- REST API Test
API & WebServices
Before going to the test let's understand what API and Webservices are,
Imagine you arrive at a restaurant and you’re wanting to have a hearty meal. There are a few steps you need to take in order to get this done.
- You perhaps take a look at the Menu to see if they can provide you with the particular sandwich that you’re craving.
- Tell the server that you’d like to order a sandwich, with bacon and cheese.
- The server delivers your order to the kitchen and the kitchen staff will make the sandwich for you, exactly how you’d like it.
- The server brings you the food, and you eat it, enjoying every bite.
- You then ask the server if you could please get the check, so you can pay for it.
- The server will go back to the Point of Sale terminal, print your check and deliver it to your table.
- You pay the server, perhaps with a tip. And you leave.
This is a very similar process as to how a Web Application works:
- The Menu you looked at before you sat down was the API Documentation for the application.
- The server in the restaurant was the API.
- The kitchen was a Web Service.
- The Point of Sale terminal was another Web Service.
An API, or Application Programming Interface is exactly that - an interface between your device or application and the Web Services that you’re trying to communicate with.
Now there are two types of API's - 1. Rest API & 2. Soap API
We will discuss about Rest API in this article.
REST API (Representational State Transfer)
REST is just a pattern for making APIs. I'd tell you what it stands for, but it doesn't matter. It's just a standard pattern for naming resources that a service provides.
In order to be able to give you a REST API definition, let’s go shopping. Let’s say you go to your favorite store to buy a new pair of jeans.
You look around and find exactly what you were looking for! But in the wrong size: It’s small while you need medium. But, unless you are a shop lifter, you do not have access to that mysterious room in the back of the store that holds all items they sell, at all sizes and colors.
To get access to this wonderland, you need to make a request to the sales person so you receive back exactly what you need: those pair of jeans you loved, but the right size for you.
So what's a REST API? It's a standard pattern for an interface for another service that you can use programmatically (from a computer program).
How to test Rest API using JMeter
Step 1 - Start JMeter & Create Thread Group
- Download JMeter from here.
- Extract the zip file in your preferred location
- In the bin folder there will be a jmeter.bat file, run it.
- Add Thread group Test plan(right click)>Add>Thread Group>New Thread Group
Step 2 - ADD HTTP sampler
- Add sampler Thread Group(right click)>Add>Sampler>HTTP Request
- We want to test weather rest api , take a sample site openweathermap.org
- This website provides free API keys to access Global Weather Data
- Sign-up in the given website to get your own unique API key. I have got mine as (b21b3bac472fce91cd28facb01b7fa8c)
- Fill details in Sampler after you get your test API request like this https://api.openweathermap.org/data/2.5/weather?q=Kolkata&appid=b21b3bac472fce91cd28facb01b7fa8c
- Server name: api.openweathermap.org
- Path: /data/2.5/weather
- There are two parameters q & appid where q is for city name & appid is for API key . Add these in the parameters field(See image for clarification).
- Now look carefully we are using GET method. This is because we are requesting a webservice through API call i.e. we are getting data from their server.
Step 3 - Run & Validate
Our test is ready, all we need is a listener to see result of the test. We will use Results Tree to validate the test.
- Add listener Thread Group(right click)>Add>Listener>View Results Tree
- Click on Start button.
We can see that whatever request we have sent through API call, we get response from their server and the response is exactly same as it was in the browser i.e. the REST API test is successful.
That's all about REST API test using JMeter. Thanks for reading.
Is there any way to validate that we have JSON in response?
Maybe be also to check if it contains coord, weather nodes?