I'm making successfull GET request using node.js and node-fetch to get an image file and save it to my computer. I am having trouble creating a file on my local machine given the Gunzip response I am getting. Here's what part of the response looks like:
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: Gunzip {
_writeState: [Uint32Array],
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
bytesWritten: 0,
_handle: [Zlib],
_outBuffer: <Buffer 00 00 00 00 00 00 00 00 47 00 00 00 00 00 00 00 00 fa 80 1b 01 00 00 00 68 ff ff ff 0a 00 00 00 ff ff ff ff 31 00 00 00 ff ff ff ff 37 00 00 00 00 00 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 2,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 3,
_info: undefined,
_maxOutputLength: 4294967296,
_level: -1,
_strategy: 0,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null,
[Symbol(kError)]: null
},
disturbed: false,
error: null
}
Any help or ideas on what I should do to create and save the proper file would be greatly appreciated!
I've tried:
const dest = fs.createWriteStream('test.png');
resp.body.pipe(dest);
dest.on("end", function(succ) {
console.log('it worked');
});
dest.on("error", function(errrr) {
console.log('failureee');
});
but It seems to save an invalid file I cannot open
Thanks to all in advanced!
Which content type is provided by server, is it correct? Your code should work provided that it is image/png.
To debug it, you can check which data the response actually provided, if it contains correct png header (it should contain PNG).