Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get byte size of request?

I am making an API in Node.js Express which might get large requests coming in. I would really like to see how big the request are.

//....
router.post('/apiendpoint', function(req, res, next) {
  console.log("The size of incoming request in bytes is");
  console.log(req.????????????); //How to get this?
});
//....
like image 357
Automatico Avatar asked Aug 30 '15 10:08

Automatico


People also ask

How many bytes is get request?

4. There should be between 54 to 66 bytes from the very start of the Ethernet frame to the ASCII “G” in the http “GET”.

How do I find HTTP request size?

To check this Content-Length in action go to Inspect Element -> Network check the request header for Content-Length like below, Content-Length is highlighted. Supported Browsers: The browsers compatible with HTTP headers Content-length are listed below: Google Chrome. Internet Explorer.

How to check request size in Node js?

You can use req. socket. bytesRead or you can use the request-stats module. So you can get the request size from details.

What is a get request in Express?

GET requests are used to send only limited amount of data because data is sent into header while POST requests are used to send large amount of data because data is sent in the body. Express. js facilitates you to handle GET and POST requests using the instance of express.


1 Answers

You can use req.socket.bytesRead or you can use the request-stats module.

var requestStats = require('request-stats');
var stats = requestStats(server);

stats.on('complete', function (details) {
    var size = details.req.bytes;
});

The details object looks like this:

{
    ok: true,           // `true` if the connection was closed correctly and `false` otherwise 
    time: 0,            // The milliseconds it took to serve the request 
    req: {
        bytes: 0,         // Number of bytes sent by the client 
        headers: { ... }, // The headers sent by the client 
        method: 'POST',   // The HTTP method used by the client 
        path: '...'       // The path part of the request URL 
    },
    res  : {
        bytes: 0,         // Number of bytes sent back to the client 
        headers: { ... }, // The headers sent back to the client 
        status: 200       // The HTTP status code returned to the client 
    }
}

So you can get the request size from details.req.bytes.

Another option is req.headers['content-length'] (but some clients might not send this header).

like image 84
Alexander Zeitler Avatar answered Sep 21 '22 06:09

Alexander Zeitler