Skip to content

Commit 9aa639b

Browse files
authored
fix: set content-length when using FormData body w/ request (#2066)
* fix: set content-length when using FormData body w/ request * import FormData
1 parent 25c3230 commit 9aa639b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/core/request.js

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Request {
182182
this.headers += `content-type: ${contentType}\r\n`
183183
}
184184
this.body = bodyStream.stream
185+
this.contentLength = bodyStream.length
185186
} else if (util.isBlobLike(body) && this.contentType == null && body.type) {
186187
this.contentType = body.type
187188
this.headers += `content-type: ${body.type}\r\n`

test/issue-2065.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const { test, skip } = require('tap')
4+
const { nodeMajor, nodeMinor } = require('../lib/core/util')
5+
const { createServer } = require('http')
6+
const { once } = require('events')
7+
const { File, FormData, request } = require('..')
8+
9+
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 8)) {
10+
skip('FormData is not available in node < v16.8.0')
11+
process.exit()
12+
}
13+
14+
test('undici.request with a FormData body should set content-length header', async (t) => {
15+
const server = createServer((req, res) => {
16+
t.ok(req.headers['content-length'])
17+
res.end()
18+
}).listen(0)
19+
20+
t.teardown(server.close.bind(server))
21+
await once(server, 'listening')
22+
23+
const body = new FormData()
24+
body.set('file', new File(['abc'], 'abc.txt'))
25+
26+
await request(`http://localhost:${server.address().port}`, {
27+
method: 'POST',
28+
body
29+
})
30+
})

0 commit comments

Comments
 (0)