API Testing (POST,GET,PUT,DELETE) (2024)

API stands for Application Programming Interface.

API is a defined set of rules, which contains clearly defined methods of communication. API helps different software components to interact with each other.

API testing involves testing the collection of APIs and checking if they meet expectations for functionality, reliability, performance, and security and returns the correct response.

API testing is used to determine whether the output is well-structured and useful to another application or not, checks the response on basis of input (request) parameter, and checks how much time the API is taking to retrieve and authorise the data too.

Postman is an application for testing APIs, by sending request to the web server and getting the response back.

  • It allows users to set up all the headers and cookies the API expects, and checks the response.
  • Productivity can be increased using some of the Postman features, which are listed below.

A test in Postman is fundamentally a JavaScript code, which run after a request is sent and a response has been received from the server.

Installing Postman

One can download Postman Native App from below URL:

OR one can add it from Google Chrome web store, one can get the Postman extension by clicking on below link:

https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en

POSTMAN is very easy to use. It provides collection of API calls, and one has to follow that collection of API calls for testing APIs of application.

One can select API call method from the given dropdown list, set Authorisation, Header, Body information according to the API call.

API call methods available in Postman:

API Testing (POST,GET,PUT,DELETE) (2)

Header according to the API call:

API Testing (POST,GET,PUT,DELETE) (3)

Body information according to the API call:

API Testing (POST,GET,PUT,DELETE) (4)

Then, one can perform the API call by clicking Send button.

Environment Variables in Postman

One can set the environments variable, from top-right corner, according to the requirement. One can easily set environment variable by following below steps:

  1. Click on Manage Environment from Settings (icon available at top right corner).
  2. Click on ADD button.
  3. Write down the Name of the Environment.
  4. Fill key & value, which can be used as variable in collection later.
API Testing (POST,GET,PUT,DELETE) (5)

Add Collection:

One can add Each API call in collection and create a collection, that will be reusable for application.

API Testing (POST,GET,PUT,DELETE) (6)

One can import collection of others and can export their collection, so that others can use it on their machine as well.

API Testing (POST,GET,PUT,DELETE) (7)
API Testing (POST,GET,PUT,DELETE) (8)

In API calls, I have mainly used two things:

1. HTTP Request — Request is the simplest way possible to make http calls.

HTTP Request contains of Request Method, Request URL, Request Headers, Request Body, Pre-request Script and Tests.

Request Methods — Request methods defines the type of request to be made.Request Methods available in Postman are as below:

API Testing (POST,GET,PUT,DELETE) (9)

I have used mainly four request methods frequently, which are as below:

  1. POST Request — For Creating Or Updating data,
  2. PUT Request — For Updating data,
  3. GET Request — For Retrieving/Fetching data and
  4. DELETE Request — For Deleting data.

Request URL — It is where to make the http request.

Request Headers — In request headers it contains key-value of the application. I have used mainly two key-value, which are as follows:

  1. Content-Type — A content-type describes the format of object data. Content-type, which I have used the most for the requests and responses is application/json.
  2. Authorization — An authorization token, included with requests, is used to identify the requester.

Request Body — It contains the data, if any (depends on type of request method), to be sent with request. I have used raw form of data for sending request. Example is as below:

API Testing (POST,GET,PUT,DELETE) (10)

Pre-request Script — Pre-request scripts are piece of code that are executed before the request is sent.

Example: In order to use Postman BDD (explained later in the article) with request, one needs to define the below code in Pre-request Script.

API Testing (POST,GET,PUT,DELETE) (11)

If the environment variable for “postman_bdd_path” is not set, then request, where pre-request script is defined, will use it from the request.

Tests in Postman — In Postman one can write and run tests for each request using the JavaScript language. Below is the example:

Example of Test Description:

API Testing (POST,GET,PUT,DELETE) (12)

Example of Test Script:

API Testing (POST,GET,PUT,DELETE) (13)

Example of Test Result:

API Testing (POST,GET,PUT,DELETE) (14)

2. HTTP Response — On sending request, API sends the response which consists of the Body, Cookies, Headers, Tests, Status code, and API Response Time.

Postman organises body and headers in different tabs. The status code with the time taken to complete the API call is displayed in another tab.

There are many status code, from which we can verify the response.

Few of them are mentioned below:

  1. 200 — For Successful request.
  2. 201 — For successful request and data was created.
  3. 204 — For Empty Response.
  4. 400 — For Bad Request. The request could not be understood or was missing any required parameters.
  5. 401 — For Unauthorized access. Authentication failed or user does not have permissions for the requested operation.
  6. 403 — For Forbidden, Access denied.
  7. 404 — For data not found.
  8. 405 — For Method Not Allowed or Requested method is not supported.
  9. 500 — For Internal Server Error.
  10. 503 — For Service Unavailable.

Test Scripts in Postman

With Postman one can write and run tests for each request using the JavaScript language. Code added under the Tests tab will be executed after response is received.

As shown in above example,

tests[“Status code is 200”] = responseCode.code ===200;

will check whether the response code received is 200.

You can have as many tests as you want for a request. Most tests are as simple and one liner JavaScript statements. Below are some more examples for the same.

Check if response body contains a string:

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

Check if response body is equal to a particular string:

tests["Body is correct"] = responseBody === "response_body_string";

Check for a JSON value:

var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;

Check for Response time is less than 200ms:

tests["Response time is less than 200ms"] = responseTime < 200;

Check for Successful POST request status code:

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

Check for Response header content type:

tests[‘The Content-Type is JSON’] = postman.getResponseHeader(‘Content-Type’) === ‘application/json’;

Overview of Postman Behavior Driven Development (Postman BDD)

Postman BDD allows to use BDD syntax to structure tests and fluent Chai-JS syntax to write assertions. So the above test cases could look like as below:

Check for Response header content type:

it(‘should return JSON’, () => {
response.should.be.json;
response.should.have.header(‘Content-Type’, ‘application/json’);
response.type.should.equal(‘application/json’);
});

Check for Status code is 200:

it(‘should be a 200 response’, () => {
response.should.have.status(200); });

Check for Response time is less than 200ms:

it(‘should respond in a timely manner’, () => {
response.time.should.be.below(200);
});

Check for Response body message should be ‘User logged in successfully.’:

it(‘message should contain’, () => {
response.body.message.should.equal(‘User logged in successfully.’) ;
});
  • Easy syntax
    It has easy syntax which makes the tests easier to write and read.
  • Error Handling
    If error in script occurs, only that one test fails, and other tests still run. Error gets displayed for the failed script.
  • Lots of Assertions
    It gives full access to all Chai-JS and Chai-HTTP assertions and some custom assertions for API. Assertions made easier to remember and readable such as a custom assertion as response.body.should.be.a.user
  • JSON Schema Validation
    User can validate responses again a specific JSON schema by using assertion as response.body.should.have.schema(someJsonSchema)

There are two simple steps to installing Postman BDD:

1. Download Postman BDD
Create a GET request in Postman with the following URL:
http://bigstickcarpet.com/postman-bdd/dist/postman-bdd.min.js

2. Install Postman BDD
User has to add the following code in the request created as above, in Tests tab:

postman.setGlobalVariable('postmanBDD', responseBody);

Then Postman BDD will be installed globally. One can use it in any Postman request by loading the script as below:

eval(globals.postmanBDD);

Conclusion

This is how I found Postman useful for API Testing and Postman BDD made my task much easier and faster.

API Testing (POST,GET,PUT,DELETE) (2024)
Top Articles
Crypto currencies have no underlying value, says RBI official
Wiktionary:What Wiktionary is not - Wiktionary, the free dictionary
Iready Placement Tables 2022-23
Boostmaster Lin Yupoo
How To Turn Off Lucky Pick On Facebook Dating
Brenda Song Wikifeet
Julian Sands Shirtless
Pjstar Obits Legacy
Walmart Careers Stocker
205-293-6392
Craiglist Boat For Sale
Richard Sambade Obituary
What does FOW stand for?
Marla Raderman 1985
Heavenly Delusion Gif
Seo Glossary definition page
In ganz Hamburg: Kommt zu diesen Side Events während des OMR Festivals 2024
State Road 38 Garage Sale Indiana 2023
The Machine 2023 Showtimes Near Cinemark Melrose Park
Top 10 Things To Do in Meridian, Mississippi - Trips To Discover
1 P.m. Pdt
Great Clips Grandview Station Marion Reviews
In The Heights Gomovies
Magicseaweed Vero Beach
Lol Shot Io Unblocked
Brake Masters 228
Electric Toothbrush Feature Crossword
Foreign Languages Building
Huff Lakjer Funeral Home
Craigslist Com San Luis Obispo
Jesus Calling: How Well Are You Listening?
Dirtyone 2006
A guide to non-religious funerals
Pella Culver's Flavor Of The Day
O'reilly's Lee Road
Sriracha Sauce Dollar General
Ups Drop Off Near By
Sayuri Pilkey
Flanner And Buchanan Obituaries Indianapolis
Winston Salem Nc Craigslist
Weather Past 3 Days
32 Movies Like Charlie and the Chocolate Factory (2005)
Shauna's Art Studio Laurel Mississippi
Blox Fruits: Best Fruits Tier List (Update 21) - Item Level Gaming
Ticket To Paradise Showtimes Near Laemmle Newhall
Ex-Wife Is Hard to Chase
Branson Shooting Range
Stranded Alien Dawn Cave Dweller
Redgifcam
Ssndob Cm New Domain
Gegp Ihub
9 Best Things To Do In Charming Surprise, Arizona
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6213

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.