Automatic Content-Length Calculation
This feature automatically calculates the Content-Length of the request body and sets it in the HTTP headers. This is useful for ensuring that the server knows the size of the incoming request payload.
const { ContentLengthMiddleware } = require('@smithy/middleware-content-length');
const { HttpRequest } = require('@smithy/protocol-http');
const request = new HttpRequest({
hostname: 'example.com',
method: 'POST',
body: JSON.stringify({ key: 'value' })
});
const middleware = ContentLengthMiddleware();
const handler = middleware((next) => async (args) => {
return next(args);
});
handler({ request }).then((response) => {
console.log(response.request.headers['Content-Length']); // Outputs the calculated Content-Length
});