Search code examples
node.jslambdarequesthttp-status-code-411

Nodejs lambda request function gets 411 Length Required


I use a node.js lambda that makes post requests to remote api, which installed on several remote locations.
I have no access to the remote api code or logs.
The lambda gets called with HTTP gateway by external application which I do not control as well.

It works perfectly for all the location but one. For one location I get this error:

411 Length Required.

I have tried to neutralize the HTTP gateway, and run lambda posts with test events.
I get the same result.

I have sent the same exact request to other locations, and got a response. I can't find the problem as I do send a ContentLength header.

This is the lambda code:

const https = require("https");
const iconv =  require("iconv-lite");
const charset  =  require("charset");
const qs  =  require("qs");
const url  =  require("url");

exports.handler = (event, context, callback) => {
  event = JSON.parse(JSON.stringify(event));

    let enc ="";
    let multiValueHeaders = event["multiValueHeaders"]["Content-Type"];
    let PostParams = null;
    let domain = event["queryStringParameters"]["domain"] ;

    let buf = Buffer.from(JSON.stringify(event["body"]), "base64"); 
    let tstring =   buf.toString("utf8");
    PostParams  = qs.parse(tstring);
     
    var postData = PostParams ?  qs.stringify(PostParams) : {};
    
    let ContentLength = new Buffer.from(postData).length;
    let headers = "" ;
    headers += (multiValueHeaders) ? (' { "Content-Type": "'+ multiValueHeaders + '",') : '{';
    headers += ('"Content-Length":'+ ContentLength + '}');
    headers = JSON.parse(headers);
   
    var q = url.parse(domain, true);
    let options = {
      'method': 'POST',
      'hostname': q.hostname,
      'path': q.pathname,
      'headers':  {headers}
    };
    
      var req = http.request(options, function (res) {
      let chunks = [];
    
      res.on("data", function (chunk) {
        chunks.push(chunk);
        enc = charset(res.headers, chunk);
      });
    
      res.on("end", function (chunk) {
          var decodedBody = iconv.decode(Buffer.concat(chunks), enc);
   
          const response = {
              statusCode: 200,
              body: decodedBody
          };
          callback(null ,response );
      });
    
      res.on("error", function (error) {
        console.error(error);
      });
    }); 
  
    
    if (PostParams != null) req.write(postData);
    req.end();  
     
  
}

When a request sent to the endpoint straight form postman there is no error. Only from lambda.


Solution

  • Apart from why this event = JSON.parse(JSON.stringify(event));?

    Apart from this is a very ugly way to build the headers object:

    let headers = "";
    headers += (multiValueHeaders) ? (' { "Content-Type": "'+ multiValueHeaders + '",') : '{';
    headers += ('"Content-Length":'+ ContentLength + '}');
    headers = JSON.parse(headers);
    

    and I would have written as:

    const headers = { "Content-Length": ContentLength };
    if(multiValueHeaders) headers["Content-Type"] = multiValueHeaders;
    

    The root cause of your problem is in this line:

    'headers':  {headers}
    

    it needs to be changed in:

    'headers': headers
    

    Hope this helps