How to download .xlsx file from AWS MWS API json response? How to download .xlsx file from AWS MWS API json response? express express

How to download .xlsx file from AWS MWS API json response?


Finally, I got a solution. I changed my method to call API as below and I got a proper excel file.

I used node-fetch and then send request to Amazon MWS by node-fetch.

node-fetch decode content encoding (gzip/deflate) properly and convert string output to UTF-8 automatically.

var param = {};  param['AWSAccessKeyId']   =  'xxxxxxxxxxxxx';   param['Action']           = 'GetReport';  param['MarketplaceId']    =  'xxxxxxxxxxxxx';   param['SellerId']         =  'xxxxxxxxxxxxx';   param['ReportId']         =  'xxxxxxxxxxxxx';   param['ItemCondition']    = 'New';   param['SignatureMethod']  = 'HmacSHA256';    param['SignatureVersion'] = '2';   param['Timestamp']        = encodeURIComponent(new Date().toISOString());  param['Version']          = '2009-01-01';   secret = 'xxxxxxxxxxxxx';   var url = [];  for(var i in param){    url.push(i+"="+param[i])  }  url.sort();  var arr = url.join("&");    var sign  = 'POST\n'+'mws.amazonservices.com\n'+'/Reports/2009-01-01\n'+arr;  const crypto = require('crypto');  let s64 = crypto.createHmac("sha256", secret).update(sign).digest('base64');  let signature = encodeURIComponent(s64);  var bodyData = arr+"&Signature="+signature;  const fileName = "sheetjs.xlsx";  var apiResponse = await fetch('https://mws.amazonservices.com/Reports/2009-01-01', {    method: 'POST',    body: bodyData,    headers: {      'content-type': 'application/x-www-form-urlencoded',       'Accept': '',    },  })  .then(res => {    const dest = fs.createWriteStream('./'+fileName);    res.body.pipe(dest).on('finish', function(){      //console.log('done');      return {'Status': 200, 'Message': 'Downloaded'};    });      })  .catch(error => {     console.log('Request failed', error);  });      return apiResponse;}