Axios:请求头缺少Content-Length头信息

7
我们在生产环境中遇到了一个问题,即使我们在头文件属性中硬编码 'Content-Length' 标头,它也没有被发送,因此我们从服务器接收到了 411 长度要求错误。
我们使用的是:
- Axios 0.16.2 - NodeJS 6.10 - 应用程序部署在 AWS Lambda 环境中
导致问题的代码如下:
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
            }
        );

我写了一个.NET应用程序,头部信息已经正确发送(无需手动传递)。这个.NET应用程序只是为了测试而编写的,并不是真正的应用程序。
你有什么想法吗? 我在axios的GitHub项目中打开了一个问题,但我想从你们那里得到一些想法。
谢谢。
2个回答

0
你尝试过向配置对象添加一个“data”字段吗?
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
        }
    );

1
看起来有些冗余。你认为它可能有帮助吗? - Denis Howe

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
  }
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接