Skip to content

Commit 6e7a455

Browse files
committedMay 1, 2018
Set content-length automatically for fs.createReadStream
·
v14.4.9v9.0.0
1 parent 00fdeea commit 6e7a455

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed
 

‎index.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,14 @@ function requestAsEventEmitter(opts) {
253253
try {
254254
uploadBodySize = await getBodySize(opts);
255255

256+
// This is the second try at setting a `content-length` header.
257+
// This supports getting the size async, in contrast to
258+
// https://github.com/sindresorhus/got/blob/82763c8089596dcee5eaa7f57f5dbf8194842fe6/index.js#L579-L582
259+
// TODO: We should unify these two at some point
256260
if (
261+
uploadBodySize > 0 &&
257262
is.undefined(opts.headers['content-length']) &&
258-
is.undefined(opts.headers['transfer-encoding']) &&
259-
isFormData(opts.body)
263+
is.undefined(opts.headers['transfer-encoding'])
260264
) {
261265
opts.headers['content-length'] = uploadBodySize;
262266
}

‎readme.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.st
114114

115115
###### body
116116

117-
Type: `string` `Buffer` `stream.Readable`
117+
Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
118118

119119
*This is mutually exclusive with stream mode.*
120120

121121
Body that will be sent with a `POST` request.
122122

123123
If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
124124

125-
If `content-length` or `transfer-encoding` is not set in `options.headers` and `body` is a string or buffer, `content-length` will be set to the body length.
125+
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
126126

127127
###### encoding
128128

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unicorns

‎test/headers.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'fs';
2+
import path from 'path';
13
import test from 'ava';
24
import FormData from 'form-data';
35
import got from '..';
@@ -115,6 +117,14 @@ test('form-data sets content-length', async t => {
115117
t.is(headers['content-length'], '157');
116118
});
117119

120+
test('stream as options.body sets content-length', async t => {
121+
const {body} = await got(s.url, {
122+
body: fs.createReadStream(path.join(__dirname, 'fixtures/stream-content-length'))
123+
});
124+
const headers = JSON.parse(body);
125+
t.is(headers['content-length'], '9');
126+
});
127+
118128
test('remove null value headers', async t => {
119129
const {body} = await got(s.url, {
120130
headers: {

0 commit comments

Comments
 (0)
Please sign in to comment.