Axios: Content-Length header missing from request headers

we are faced with a production problem that the Content-Length header is not sent even if we hard-code it in the header properties, so we get a 411 Length Required error from the server back.

We use:

  • Axios 0.16.2
  • NodeJS 6.10
  • AWS Lambda application deployed

The code causing the problem is as follows:

let cookieJar;
const postBody = "MyBody=MyBodyContentGoesHere";
const url = "https://my-url.com";
return axios
        .post(url,
            postBody,
            {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                timeout: 10000,
                jar: cookieJar,
                withCredentials: true
            }
        );

I wrote the application in .NET and the header was sent correctly (without manually passing it). This .NET application was written just for verification, it is not a real application.

Do you have an idea? I open the problem in the axios github project, but I want to know from you guys some ideas.

Thank.

+4
source share
2 answers

?

let cookieJar;
const postBody = "MyBody=MyBodyContentGoesHere";
const url = "https://my-url.com";
return axios
    .post(url,
        postBody,
        {
            data: postBody,
             headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            timeout: 10000,
            jar: cookieJar,
            withCredentials: true
        }
    );
0

, , querystring .

'use strict';

let querystring = require('querystring');

let cookieJar;
const postBody = querystring.stringify({ MyBody: 'MyBodyContentGoesHere' });
const url = 'https://my-url.com';
return axios.post(url, postBody, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    timeout: 10000,
    jar: cookieJar,
    withCredentials: true
  }
);
0

Source: https://habr.com/ru/post/1688165/


All Articles