Open ronag opened 2 years ago
As far as I can see there is no reason for this to use chunked-encoding.
Thanks for the answer.
It seems like the issue is that the spec is unclear https://fetch.spec.whatwg.org/#concept-bodyinit-extract
I checked Firefox and it looks like they do set the length in their implementation https://searchfox.org/mozilla-central/rev/8dd35cd8f5284fbaa506aab02fe42fc87efb249e/dom/html/HTMLFormSubmission.cpp#373
@thatsmydoing PR welcome!
This was fixed in f38fbdc0f03d5c19d74d09bc0bee32c58a5afd6f
Just notice something...
here are one other reason of why i prefer the FormData -> Blob conversions more. The formdata has to be immutable see failing test in NodeJS:
const fd = new FormData()
fd.set('foo', 'bar')
const req = new Request('https://httpbin.org/post', {
method: 'POST',
body: fd
})
fd.set('foo', 'foo')
// fd.set('foo', 'foooo') could also set the field to a other size making req content-length invalid.
const fd2 = await req.formData()
console.log(fd2.get('foo')) // prints foo in NodeJS and bar in browsers.
When you convert the formdata to a blob then you will already have strings converted to uint8arrays and they stay immutable. from the blob you also get
yield * blob.stream()
Would you be willing to make a PR implementing the FormData -> Blob changes?
sure
hmm. couldn't really convert the formdata to a blob as i'm doing in node-fetch and the formdata polyfill for browsers
I'm using fetch-blob
instead that can accept 3rd party blobs backed up by the fs.
And i couldn't really use the FileLike
as it did only accept one blob part instead of an array of parts.
So it's not spec compatible in that regards...
import { fileFromSync } from 'fetch-blob/from.js'
const fsFile = fileFromSync('file.txt')
const fd = new globalThis.FormData()
fd.set('a', fsFile)
const FileLike = fd.get('a').constructor
new FileLike(['a', 'b']) // don't work...
new FileLike([blob, blob]) // don't work either...
undici.fetch()
is good but undici.request()
still send with transfer-encoding: chunked
.
const undici = require("undici");
(async () => {
const formData = new undici.FormData();
formData.append("myname", "myvalue");
const res = await undici.fetch("http://localhost:8181", {
method: "POST",
body: formData,
});
})();
$ nc -l 8181
POST / HTTP/1.1
host: localhost:8181
connection: keep-alive
content-type: multipart/form-data; boundary=----formdata-undici-062462082415
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: undici
accept-encoding: gzip, deflate
content-length: 130
------formdata-undici-062462082415
Content-Disposition: form-data; name="myname"
myvalue
------formdata-undici-062462082415--
const undici = require("undici");
(async () => {
const formData = new undici.FormData();
formData.append("myname", "myvalue");
const res = await undici.request("http://localhost:8181", {
body: formData,
});
})();
$ nc -l 8181
PUT / HTTP/1.1
host: localhost:8181
connection: keep-alive
content-type: multipart/form-data; boundary=----formdata-undici-008681374567
transfer-encoding: chunked
5e
------formdata-undici-008681374567
Content-Disposition: form-data; name="myname"
myvalue
24
------formdata-undici-008681374567--
0
Thank you so much for reopening.
Is the undici has stable version? The file uploading is not work at all in nodejs 20 I upload successful using axios, but the undici always has bug
Discussed in https://github.com/nodejs/undici/discussions/1420