After only few days of using Postman, once we were done with
exploring the basic Postman features, felt a need to query the application
database. We were exposing Rest endpoints for many modules of our enterprise
application and validating data persistence after a POST call was a necessity.
Other API testing tools (SoapUI/JMeter) we were using had this capability and
we were expecting this in Postman as well. After much googling realized there
is no direct way to do database query in Postman.
So we thought of creating a Restful service to perform the
database query and calling that service from our Postman collection. We wrote a
simple node.js program to run as a Restful service in an express server. We
were anyway using node.js to run our collection using Newman - so no additional
requirement of installing new software/managing a server to host the service
using this approach. Given below the steps to achieve our objective:
Step 1: Install node.js if not already installed.
[Download relevant installer from https://nodejs.org/en/download/
and install]
Step 2:
Install 'express', 'mysql' and 'body-parser' using below
commands from command prompt:
npm install express
npm install mysql
npm install body-parser
Step 3:
Save the below program in a directory naming it
"connect_mysql.js":
var express = require('express');
var app = express();
const bodyParser = require('body-parser');
var mysql = require('mysql');
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/sendQuery', function (req, res) {
console.log('Host:', req.body.host)
console.log('User:', req.body.user)
console.log('Password:', req.body.password)
console.log('Query:', req.body.query)
var connection = mysql.createConnection({
host : req.body.host,
user : req.body.user,
password : req.body.password,
database : req.body.database
});
connection.query(req.body.query, function(err, rows, fields) {
if (!err){
console.log('Result Set: ', rows);
res.send(rows)
}
else
console.log('Error while performing Query.');
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(host)
console.log("Example app listening at port %s", port)
})
Step 4:
Run the program to start the service from command prompt:
node connect_mysql.js
[We created a batch file to launch Postman and start this
service together]
Step 5:
In Postman add a POST request to
http://localhost://8081/sendQuery with "x-www-form-urlencoded"
payload as shown below:
Send the POST request.
Hope this will help some of you to perform database validation from Postman/Newman. Thank you.
This is a very informative article. Thanks for getting this together!!!
ReplyDeleteA nice article and will be of great help to perform database validation.
ReplyDeleteNice blog! I read your article. I really like all the points you mentioned are interesting and useful. Thanks for sharing this blog.
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
Glad to know you liked it.Thanks!
ReplyDeleteMy database is a remote Microsoft Server. How would I modify your technique to select data form it? I have already verified that I can select data from the target DB using sqlcmd on the same PS where the Node.js server will be running.
ReplyDeleteSorry for delayed response. Didn't get a chance to look into for quite sometime. In my case also SQL Server was a remote one. Just provide the details of your Database server as shown in "Step5". Thanks.
DeleteSorry, to be clear, a remote "Microsoft SQL Server".
ReplyDeletehow to integrate with Jenkins?I have my postman collection in a branch in github.If I want to kick off a build for that branch then what needs to be done
ReplyDelete