1

I'm trying to make an API request using HttpClient to download a CSV file. I had to switch from Axios to HttpClient due to ForgeRock authentication that requires a bearer token. I'm having an issue where the response coming back does not have response.data. Where would I find the data to download to CSV?

My code:

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);
      })
  }

The response I'm getting back is the below. The response does not have "response.data" as an object key. When I open the CSV it only has "undefined". enter image description here

This is my response.body: enter image description here

1 Answer 1

1

You are using response.data instead of response.body? Thats why it says undefined. Remember CSV is just plain text and response.data is undefined.

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);
        })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Looks like the response has a body field and not data. So do console.log(response.body) and see if it shows what you're expecting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.