英文:
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.
But when I use Postman's Send and Download button to call that external api I can get the xlsx file to save.
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.
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.
答案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);
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论