如何从axios发送的POST请求中获取Content-Length标头?

时间:2019-10-08 11:54:27

标签: javascript http-headers axios httprequest interceptor

我想使用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头文件吗?如果没有,是否可以从其他任何地方访问它?

1 个答案:

答案 0 :(得分:0)

内容长度将自动按http adapter添加。

相关问题