Skip to content

Commit de9514d

Browse files
committed
Set content-length header when using form-data
Fixes #466
1 parent 82763c8 commit de9514d

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ function requestAsEventEmitter(opts) {
254254
Promise.resolve(getBodySize(opts))
255255
.then(size => {
256256
uploadBodySize = size;
257+
258+
if (
259+
is.undefined(opts.headers['content-length']) &&
260+
is.undefined(opts.headers['transfer-encoding']) &&
261+
isFormData(opts.body)
262+
) {
263+
opts.headers['content-length'] = size;
264+
}
265+
257266
get(opts);
258267
})
259268
.catch(err => {

test/headers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ test('form-data automatic content-type', async t => {
105105
t.is(headers['content-type'], `multipart/form-data; boundary=${form.getBoundary()}`);
106106
});
107107

108+
test('form-data sets content-length', async t => {
109+
const form = new FormData();
110+
form.append('a', 'b');
111+
const {body} = await got(s.url, {body: form});
112+
const headers = JSON.parse(body);
113+
t.is(headers['content-length'], '157');
114+
});
115+
108116
test('remove null value headers', async t => {
109117
const headers = (await got(s.url, {
110118
headers: {

0 commit comments

Comments
 (0)