0

我正在尝试使用 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”作为对象键。当我打开 CSV 时,它只有“未定义”。 在此处输入图像描述

这是我的回应。正文: 在此处输入图像描述

4

1 回答 1

0

您正在使用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);
      })
  }
于 2020-06-03T06:28:32.100 回答