簡體   English   中英

如何從使用 axios 發送的 POST 請求中獲取內容長度 header?

[英]How to get the Content-Length header from a POST request send with axios?

我想使用我的 POST 請求的大小和數據。 我正在做一些研究,發現了一個請求的 Content-Length header,但我在我的 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 給我內容長度 header? 如果沒有,有沒有辦法從其他任何地方訪問它?

content-length 將由http 適配器自動添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM