我正在使用axios
从外部API获取响应中的Content-Disposition
头。
尽管Chrome DevTools网络响应中存在该标头,但似乎无法从服务器获得该特定标头的访问权限。
我发现了这篇文章,讲述了如何通过Access-Control-Expose-Headers
公开Content-Disposition标头,但我不太确定如何在Nextjs中实现它。
我按照此Nextjs文档页面有关安全标头的说明编辑了next.config.js
文件,但没有成功。
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
async headers() {
// to allow specific headers to appear in requests
// https://nextjs.org/docs/advanced-features/security-headers
const securityHeaders = [
// important
{ key: "Access-Control-Expose-Headers", value: "Content-Disposition" },
]
return [
{
source: '/:path*', // req path
headers: securityHeaders
}
]
}
}
以下是我使用axios进行的API调用:
// lib/utils.js
export async function downloadFile(collectionName: string, documentId: string) {
const res = await axios.get(
`https://api.myapiendpoint.com/file/${collectionName}/${documentId}`
);
console.log(res.headers);
}
console.log 输出:
// these are the only headers I receive
{
"content-length": "195687",
"content-type": "text/csv; charset=utf-8",
"last-modified": "Thu, 10 Feb 2022 16:00:05 GMT"
}