javascript - How to pass POST data using apache benchmark in node js API - Stack Overflow

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

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 mand 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 

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 mand?

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 mand 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./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 mand?

Share Improve this question edited Dec 11, 2019 at 19:56 Olaf Kock 48.2k9 gold badges62 silver badges91 bronze badges asked Dec 11, 2019 at 5:02 Deep KakkarDeep Kakkar 6,3054 gold badges43 silver badges80 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

content type should be application/json instead of application/x-www-form-urlencoded

try like this,

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

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 e 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744499128a4577324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信