[英]Download CSV files with HttpClient in ReactJS
我正在嘗試使用 HttpClient 發出 API 請求以下載 CSV 文件。 由於 ForgeRock 身份驗證需要不記名令牌,我不得不從 Axios 切換到 HttpClient。 我遇到了返回的響應沒有 response.data 的問題。 我在哪里可以找到要下載到 CSV 的數據?
我的代碼:
fetchCSV = date => {
const url = "https://<URLdownloadLink>?runDate="+date;
const method = "GET";
HttpClient
.request({
url,
method,
bypassAuthentication: false,
responseType: 'blob',
init: {},
timeout: 5000
})
.then((response) => {
console.log("data", response)
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'Asset Manager.csv');
document.body.appendChild(link);
link.click();
link.remove();
})
.catch(error => {
console.log("error", error);
})
}
我得到的回復如下。 響應沒有“response.data”作為 object 密鑰。 當我打開 CSV 時,它只有“未定義”。
您正在使用response.data
而不是response.body
? 這就是為什么它說undefined
。 記住 CSV 只是純文本, response.data
是未定義的。
fetchCSV = date => {
const url = "https://<URLdownloadLink>?runDate="+date;
const method = "GET";
HttpClient
.request({
url,
method,
bypassAuthentication: false,
responseType: 'blob',
init: {},
timeout: 5000
})
.then((response) => {
console.log("data", response)
const url = window.URL.createObjectURL(new Blob([response.body]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'Asset Manager.csv');
document.body.appendChild(link);
link.click();
link.remove();
})
.catch(error => {
console.log("error", error);
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.