Search code examples
javascriptnode.jsbenchmarkingapachebench

How to pass POST data using apache benchmark in node js API


I am calling API built-in node js and passing the body with the POST method as JSON. The JSON file contains the parameters as file wallet_view.json

{"version":4.2,"auth_token":"0625d8042e2b2a04c5770f54a0c7a83d","page":1}

and the index file of node js:

const express = require('express');
const app = express();
const process = require('process');
const config = require('./config/config.js');

app.use(express.raw());
app.use(express.urlencoded({ extended: true }));

process.on('uncaughtException', function (error) {
    console.log('Uncaught Exception: ' + error);
});

app.use((req, res, next) => {
    console.log(req.originalUrl);
    console.log(req.body);
    const routes_handler = require('./routes/index.js')(app, express, req);
    next();
});

app.listen(config.SERVER.PORT, () => {
    console.log("Server running at Port " + config.SERVER.PORT);
});

The command which I am using is as follows:

ab -n 2 -c 1 -T 'application/x-www-form-urlencoded' -p ./wallet_view.json -A "username:password" -v 4 http://domainname.com/wallet/view

But, now in console log of req.body, I am getting the output as

{ '"version":4.2,"auth_token":"0625d8042e2b2a04c5770f54a0c7a83d","page":1': '' }

which is the wrong JSON format. This way I am unable to get req.body.auth_token. Can anyone please suggest what kind of modifications should I do in ab command?


Solution

  • I got it, It was the issue in the request format which I was passing. I was passing the request as JSON

    {"version":4.2,"auth_token":"0625d8042e2b2a04c5770f54a0c7a83d","page":1}
    

    As the API request will always come as form-urlencoded and in Node.js file, I am using express.raw()

    app.use(express.raw());
    app.use(express.urlencoded({ extended: true })); 
    

    so it was the wrong format. As I am using content-type as 'application/x-www-form-urlencoded'.

    So I should use the request format as

    version=4.2&auth_token=0625d8042e2b2a04c5770f54a0c7a83d&page=1
    

    This way I handled the problem. Thank you.