undici icon indicating copy to clipboard operation
undici copied to clipboard

How can I use fixed-length multipart request with FormData?

Open ronag opened this issue 2 years ago • 12 comments

Discussed in https://github.com/nodejs/undici/discussions/1420

Originally posted by nwtgck May 6, 2022 Hi, I'd like to request a fixed-length multipart request. The code below requested with transfer-encoding: chunked without content-length. I assume the length of formData is statically decided.

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 -lp 8181
PUT / HTTP/1.1
host: localhost:8181
connection: keep-alive
content-type: multipart/form-data; boundary=----formdata-undici-0.5140957512643098
transfer-encoding: chunked

64
------formdata-undici-0.5140957512643098
Content-Disposition: form-data; name="myname"

myvalue

2a
------formdata-undici-0.5140957512643098--
0
```</div>

ronag avatar May 06 '22 12:05 ronag

As far as I can see there is no reason for this to use chunked-encoding.

ronag avatar May 06 '22 12:05 ronag

Thanks for the answer.

nwtgck avatar May 06 '22 12:05 nwtgck

It seems like the issue is that the spec is unclear https://fetch.spec.whatwg.org/#concept-bodyinit-extract

https://github.com/nodejs/undici/blob/8549a9402c46034d6d28b2f6f65d934736e51a59/lib/fetch/body.js#L114-L115

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 avatar Aug 26 '22 07:08 thatsmydoing

@thatsmydoing PR welcome!

ronag avatar Aug 26 '22 07:08 ronag