从我的Node.js服务器调用的外部API获取的xlsx文件,无需保存即可读取。

huangapple go评论84阅读模式
英文:

Read a xlsx file without saving, which is coming from external api called from my server in nodejs

问题

I apologize for the confusion earlier. Here's the translated code part without any additional content:

const https = require('https');
var postData = JSON.stringify({
    'msg': 'Hello World!'
});
let url;
url = "someurl2228.com"; //dummy/random url (have removed actual url)
var options = {
    hostname: url,
    path: '/file/download/b8d61eff5314ac1ac982e5dbb9630f83',
    method: 'GET',
    headers: {
        myheader: 'somedata'
    }
};
var req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);
    res.on('data', (d) => {
        process.stdout.write(d);
    });
    res.on('end', () => {
        console.log('********************END********');
    });
});
req.on('error', (e) => {
    console.error(e);
});
req.write(postData);
req.end();

Please note that the code you provided is for making an HTTP GET request to a specific URL and logging the response to the console. If you want to process the XLSX file in the response, you will need to use a library like xlsx in Node.js to parse and work with the Excel data.

英文:
const https = require('https');
var postData = JSON.stringify({
    'msg' : 'Hello World!'
});
let url;
url = "someurl2228.com";//dummy/random url (have removed actual url)
var options = {
  hostname: url,
  path: '/file/download/b8d61eff5314ac1ac982e5dbb9630f83',
  method: 'GET',
  headers: {myheader:"somedata"}
};
var req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);
  res.on('data', (d) => {
    process.stdout.write(d);
  });
  res.on('end',()=>{
    console.log('********************END********');
  });});
req.on('error', (e) => {
  console.error(e);});
req.write(postData);
req.end();

I have removed actual urls. that is some just random url in my code above.

From my code I am using https module to call a external rest api which in response sends me a xlsx file.
I want to read the file (without saving will be great).

Now, when I use Postman's Send button to call that external api I receive a unicode chracters in respose tab.
从我的Node.js服务器调用的外部API获取的xlsx文件,无需保存即可读取。
But when I use Postman's Send and Download button to call that external api I can get the xlsx file to save.
从我的Node.js服务器调用的外部API获取的xlsx文件,无需保存即可读取。

Now, I am able to replicate same unicode as in I am receiving with Postman's Send button in my NodeJS code using https module but don't know what to do next.
从我的Node.js服务器调用的外部API获取的xlsx文件,无需保存即可读取。

I want to read the xlsx file's data for further task.

Want to read the xlsx file's data without saving it to do further task.

Also Adding a snippet of my excel
从我的Node.js服务器调用的外部API获取的xlsx文件,无需保存即可读取。

答案1

得分: 0

你可以将结果收集到一个缓冲区中,然后将结果传递给XLSX的.read方法(然后在内存中读取它)。

尝试这样做:

var req = http.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);
  
  // 在这里存储缓冲区
  let chunks = [];

  res.on('data', (d) => {
    chunks.push(d);
  });
  res.on('end', () => {
    console.log('********************END********');

    // 将缓冲区传递给`.read`方法
    const workbook = require('xlsx').read(chunks, {type: 'buffer'});

    console.log('No more data in response.\nDo something with the workbook:\n\n', workbook);
  });
});
英文:

You could collect the result as a buffer and then pass the result to the XLSX's .read method (which will then read it in memory).

Try this:

var req = http.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);
  
  // store buffer here
  let chunks = [];

  res.on('data', (d) => {
    chunks.push(d);
  });
  res.on('end', () => {
    console.log('********************END********');

    // feed buffer to `.read` method
    const workbook = require('xlsx').read(chunks, {type: 'buffer'});

    console.log('No more data in response.\nDo something with the workbook:\n\n', workbook);
  });
});

huangapple
  • 本文由 发表于 2023年1月10日 02:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75061665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定