【问题标题】:Axios: Content-Length header is missing on request headersAxios:请求标头中缺少 Content-Length 标头
【发布时间】:2018-04-06 16:12:18
【问题描述】:

我们在生产中面临一个问题,即“Content-Length”标头没有被发送,即使我们在标头属性中对其进行硬编码,所以我们从服务器收到 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 中编写了一个应用程序,并且标头已正确发送(无需手动传递)。这个 .NET 应用程序只是为了测试而编写的,它不是真正的应用程序。

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

谢谢。

【问题讨论】:

    标签: node.js axios content-length


    【解决方案1】:

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

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

    【讨论】:

    • 似乎是多余的。为什么你认为它可能会有所帮助?
    【解决方案2】:

    我和你有同样的问题。按照the offical docs 中的建议使用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
      }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 2019-11-21
      • 2018-11-02
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多