Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch - POST - use stream as body? #4126

Closed
KevinSoulat opened this issue Mar 17, 2023 · 8 comments
Closed

Fetch - POST - use stream as body? #4126

KevinSoulat opened this issue Mar 17, 2023 · 8 comments

Comments

@KevinSoulat
Copy link

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
Copy link
Contributor

@KevinSoulat
Copy link
Author

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

@preveen-stack
Copy link
Contributor

preveen-stack commented Apr 25, 2023

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?

@KevinSoulat
Copy link
Author

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.

Copy link

github-actions bot commented May 7, 2024

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 github-actions bot added the stale label May 7, 2024
@purplesyringa
Copy link

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

Copy link

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.

Copy link

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 github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants