alexfernandez / loadtest

Runs a load test on the selected URL. Fast and easy to use. Can be integrated in your own workflow using the API.
MIT License
2.55k stars 206 forks source link

no body sent #202

Closed toguvr closed 11 months ago

toguvr commented 2 years ago

i am trying to do a request test but my body isnt sending...

` const loadtest = require("loadtest"); function contentInspector(result) { if (result.statusCode == 200) { const body = JSON.parse(result.body); // how to examine the body depends on the content that the service returns if (body.status.err_code !== 0) { result.customError = body.status.err_code + " " + body.status.msg; result.customErrorCode = body.status.err_code; console.log(result.customErrorCode); } } }

const body = { email: "augusto@gmail.com", password: "123456", };

const options = { url: "https://localhost:3000/sessions", maxRequests: 10, concurrency: 20, method: "POST", requestsPerSecond: 500, contentInspector, body, };

loadtest.loadTest(options, function (error, result) { console.log(result); if (error) { return console.error("Got an error: %s", error); } console.log("Tests run successfully"); });

`

rmkamaleev commented 2 years ago

Don't know if it's still relevant but here is the solution I've found. If you don't get the body in req.body for your server, it's most likely due to the fact that loadtest sends raw body by chunks Therefore you need to read it like this also

Here is the code for your node.js express server that will help:

app.use(function(req, res, next) {
  req.rawBody = '';
  req.setEncoding('utf8');

  req.on('data', function(chunk) { 
    req.rawBody += chunk;
  });

  req.on('end', function() {
    next();
  });
});

You can add it to all the routes with .use or just a single route by passing it as the middleware function

With this added you can get the req.body via accessing req.rawBody for your route

alexfernandez commented 11 months ago

Thanks for the answer, closing.