Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

551
Views
A GET API (google sheets API) returns an excel sheet in windows 1252 encoding format. How can I download the sheet as excel using nodeJS?

This is the response API gives

This is the code snippet.

axios.get(api , { headers: {"Authorization" : `Bearer ${token}` , "Content-Type":"application/json" , "Accept" : "*/*", "Accept-Encoding" : "gzip, deflate, br"} })
  .then(res => {
  try {     
  let fileString = iconv.decode(res.data, 'win1252');
  fs.writeFile('NodeReport.xlsx' , fileString , (error) => {
    console.log(error);
  });

  }
  catch (err){
    console.log(err);
  }
  }

);

So if I hit the API in browser it downloads the excel file , and If I save response from Postman as file it also downloads excel file . But I want to hit the API from my nodeJS code and then save the file as Excel file . I have tried to use fs.writeFile but it doesn't open the saved file , it says the format is wrong or content is corrupted. Can anyone tell how can I save this file from API response ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your script, as a modification, how about the following modification?

Modified script:

axios
  .get(api, {
    headers: { Authorization: `Bearer ${token}` },
    responseType: "arraybuffer",
  })
  .then((res) => {
    try {
      fs.writeFile("NodeReport.xlsx", res.data, (error) => {
        console.log(error);
      });
    } catch (err) {
      console.log(err);
    }
  });
  • In this modification, the data is retrieved as the array buffer and saved the data as an XLSX file.

  • If the exported file is broken, please modify fs.writeFile("NodeReport.xlsx", res.data, (error) => { to fs.writeFile("NodeReport.xlsx", new Buffer.from(res.data), (error) => { and test it again.

Reference:

  • axios
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!