node.js - Axios:请求 header 中缺少内容长度 header

标签 node.js axios content-length

我们在生产中面临一个问题,即“Content-Length” header 没有被发送,即使我们在 header 属性中对其进行硬编码,所以我们从服务器收到 411 Length Required 错误。

我们正在使用:

  • 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 中编写了一个应用程序, header 已正确发送(无需手动传递)。这个 .NET 应用程序只是为了测试而编写的,它不是真正的应用程序。

你有什么想法吗? 我在 axios github 项目中打开了一个问题,但我想从你们那里了解一些想法。

谢谢。

最佳答案

您是否尝试过向配置对象添加“数据”字段?

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

关于node.js - Axios:请求 header 中缺少内容长度 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46934653/

相关文章:

javascript - 用于更新 JSON 文件的 Node.JS 服务器端脚本

javascript - 在 Node.js 中,如何防止客户端从我的网站打开多个实例

node.js - mongoose 到 mlab 连接问题 : UnhandledPromiseRejectionWarning: Unhandled promise rejection

node.js - docker 与 axios (node) 组成网络错误 ENOTFOUND

reactjs - axios 发布错误

api - 使用Axios进行API调用时Express中的错误处理

java - 如何获取 InputStream 的大小?

java - 如何在android中设置内容长度?

javascript - 单元测试中的 Passport JWT 身份验证 - 套接字挂断

c - 获取 float 的长度