How to get the Axios's response file type All In One
获取 Axios 请求响应头中的文件类型
Axios API
https://www.npmjs.com/package/axios
demos
import fs from 'node:fs';
import path from 'path';
import axios from 'axios';
async function downloadFile(url, path) {
const writer = fs.createWriteStream(path);
const res = await axios({
url,
method: "GET",
responseType: "stream",
});
// console.log(`res =`, res)
console.log(`✅ content-type =`, res.headers['content-type'])
res.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp4`
await downloadFile(url, "./test.mp4");
// ✅ content-type = video/mp4
/*
$ node ./file-type.js
*/
import axios from 'axios';
async function getUser() {
try {
const response = await axios.get(
'https://api.xgqfrms.xyz/api/users',
);
console.log(response.headers);
console.log(response.headers['date']);
console.log(response.headers['etag']);
console.log(response.headers['connection']);
console.log(response.headers['content-type']);
return response.data;
} catch (err) {
console.log(err);
}
}
console.log(await getUser());