Fetch - POST - use stream as body?
KevinSoulat opened this issue · 7 comments
Details
Hi guys,
Related to this post #4118, is there a way to use a readable stream as body of POST (or PUT) request, to avoid memory issues with large files?
// helpers
const createBlobFromFile = async path => new Blob([ await fs.promises.readFile(path) ]); // suboptimal
const body = new FormData();
//body.append('upload', fs.createReadStream(filePath)); // doesn't work
body.append('upload', await createBlobFromFile(filePath), 'myFile.wav' );
const response = await fetch(url, { method: 'POST', body })
const result = await response.json();
console.log( response.status, result )
Thanks
Node.js version
v18
Example code
No response
Operating system
windows/mac
Scope
code
Module and version
Not applicable.
@preveen-stack, not sure what you meant exactly.
I tried different encoding, but it didn't work.
It's possible to use stream with http module as explained here: https://stackoverflow.com/a/64002066, but I wonder if it's possible with fetch().
const fs = require('fs');
const fetch = require('node-fetch');
const fileStream = fs.createReadStream('/path/to/large/file.wav);
fetch('https://example.com/api', {
method: 'POST',
body: fileStream
}).then(response => {
// handle response
});
Can you check this code?
Hi @preveen-stack, this code doesn't work (for instance require('node-fetch') can't work since it's a pure ESM module), and my point is to use the native "fetch" module and not the tier-party module "node-fetch" to do that.
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
I'm also interested in this feature. Chromium supports something similar, so this is not without a precedent.
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.