help icon indicating copy to clipboard operation
help copied to clipboard

Fetch - POST - use stream as body?

Open KevinSoulat opened this issue 2 years ago • 6 comments

Details

Hi guys,

Related to this post https://github.com/nodejs/help/issues/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.

KevinSoulat avatar Mar 17 '23 21:03 KevinSoulat

please check https://nodejs.org/api/fs.html#filehandlecreatereadstreamoptions

preveen-stack avatar Apr 21 '23 08:04 preveen-stack

@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().

KevinSoulat avatar Apr 21 '23 20:04 KevinSoulat

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?

preveen-stack avatar Apr 25 '23 15:04 preveen-stack

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.

KevinSoulat avatar Apr 25 '23 16:04 KevinSoulat

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.

github-actions[bot] avatar May 07 '24 01:05 github-actions[bot]

I'm also interested in this feature. Chromium supports something similar, so this is not without a precedent.

purplesyringa avatar May 16 '24 16:05 purplesyringa

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.

github-actions[bot] avatar Nov 28 '24 01:11 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. 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.

github-actions[bot] avatar Dec 29 '24 01:12 github-actions[bot]