Steps to Create JSON Server On the Fly and REST API Testing using Postman QA Tool
Steps to Create JSON Server On the Fly and API Testing
using Postman QA Tool
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1)
Create New Folder with any name, for e.g: JSONServerDemo
2)
Open the JSONServerDemo folder in VS code Or Open with cmd prompt, move your
cursor to the current directory
3)
Install JSON Server
npm install -g json-server
4)
Start JSON Server with below cmd, This will create automatically db.json file
with built in Posts, comments, profile json data
json-server --watch db.json
5)
Go db.json file and add below Employee JSON array
"employees":
[
{
"id": 1,
"name":
"Pankaj",
"salary":
"10000"
},
{
"id": 2,
"name": "David",
"salary":
"5000"
}
]
6)
Open below links in browser for CRUD operations
http://localhost:3000/
GET
http://localhost:3000/employees
-----------------------------------------------------------------------------------------------------
Open
POSTMAN and test for PUT, DELETE, POST, GET operations
-----------------------------------------------------------------------------------------------------
Creating
New Collection in Postman
----------------------------------------------------------------------------------------------
Open
Postman tool and then Click on New option. Select Collection to create new
collection
Give some name, for e.g: Employees Collection and then click on Create Button
GET http://localhost:3000/employees
---------------------------------------------------
---------------------------------------------------
O/P:
{
"id":
1,
"name":
"Pankaj",
"salary":
"10000"
},
{
"id": 2,
"name":
"David",
"salary":
"5000"
}
----------------------------------------------------------------------------------
GET http://localhost:3000/employees/1
---------------------------------------------------
---------------------------------------------------
O/P:
{
"id":
1,
"name":
"Pankaj",
"salary":
"10000"
}
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
POST http://localhost:3000/employees
-----------------------------------------------
Inside
PostMan Tool -> Under Body -> Select RAW ->
Select JSON type
{
"name": "John",
"salary": "25000"
}
---------------------------------------------------
O/P:
{
"id": 3,
"name": "John",
"salary": "25000"
}
----------------------------------------------------------------------------------
PUT http://localhost:3000/employees/3
---------------------------------------------------
Inside
PostMan Tool -> Under Body -> Select RAW ->
Select JSON type
{
"id": 3,
"name": "Mukunda",
"salary": "35000"
}
---------------------------------------------------
O/P:
{
"id": 3,
"name": "Mukunda",
"salary": "15000"
}
--------------------------------------------------------------------------------------------
DELETE http://localhost:3000/employees/3
---------------------------------------------------------------------
Call Get All Employees Request again
---------------------------------------------------
O/P:
[
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"id": 2,
"name": "David",
"salary": "5000"
}
]
Thank
you for reading and referring my blog. Kindly share your comments. 😊
Linked In Profile: https://www.linkedin.com/in/narendar-l-gentyala-521754b8/
This blog will help you to understand, how easily you can create REST API using "json-server" npm module. Testing REST API using Postman QA Tool. I will share in another blog, How to do REST API Testing using BASIC Authentication and OAuth Authentication. Thank you.
ReplyDeleteReally nice blog Narendar sir. :)
DeleteIts great sir :)
DeleteNice sir. ..
ReplyDeleteThank you
Simple and easy way to implementation.
ReplyDeleteThank you sir
Cybersecurity Awareness | Are You Secure? – ProtectBD
ReplyDeleteCybersecurity Awareness: we live in a world where cyberattacks bring real threats to you and your devices. Learn details about cybersecurity!