【问题标题】:How to get the Content-Length header from a POST request send with axios?如何从使用 axios 发送的 POST 请求中获取 Content-Length 标头?
【发布时间】:2020-02-05 17:04:49
【问题描述】:

我想使用我的 POST 请求的大小和数据。我正在做一些研究,发现了一个请求的 Content-Length 标头,但我在我的 axios 请求标头中找不到它。

我尝试过使用拦截器,就像这样:

axios.interceptors.request.use(
    config => {
        console.log('config', config.headers);
        if (config.url != `${API_URL}/login`)
            config.headers.Authorization = 'Bearer ' + getAccessToken();
        return config;
    },

    error => {
        return Promise.reject(error);
    }
);

这是我得到的回应:

Authorization: "Bearer [...access_token]"
Content-Type: "multipart/form-data"
common:
    Accept: "application/json, text/plain, */*"
    X-CSRF-TOKEN: "..."
    X-Requested-With: "XMLHttpRequest"
    __proto__: Object
delete: {}
get: {}
head: {}
patch: {Content-Type: "application/x-www-form-urlencoded"}
post: {Content-Type: "application/x-www-form-urlencoded"}
put: {Content-Type: "application/x-www-form-urlencoded"}

但在 Chrome 中显示的是这样的:

Accept: application/json, text/plain
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ro;q=0.8,la;q=0.7
Authorization: Bearer [...access_token]
Connection: keep-alive

Content-Length: 5266672 <---- this is what I need

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryGMzak87LIZH05nme
Cookie: XSRF-TOKEN= ...
Host: ...
Origin: ...
Referer: ...
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
X-CSRF-TOKEN: ...
X-Requested-With: XMLHttpRequest
X-XSRF-TOKEN: ...

有什么方法可以让 axios 给我 content-length 标头吗?如果没有,有什么方法可以从其他任何地方访问它吗?

【问题讨论】:

    标签: javascript http-headers axios httprequest interceptor


    【解决方案1】:

    content-length 将由 http adapter 自动添加。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      相关资源
      最近更新 更多