File tree Expand file tree Collapse file tree 2 files changed +13
-7
lines changed Expand file tree Collapse file tree 2 files changed +13
-7
lines changed Original file line number Diff line number Diff line change @@ -162,12 +162,10 @@ module.exports = options => {
162
162
try {
163
163
uploadBodySize = await getBodySize ( options ) ;
164
164
165
- if (
166
- uploadBodySize > 0 &&
167
- is . undefined ( options . headers [ 'content-length' ] ) &&
168
- is . undefined ( options . headers [ 'transfer-encoding' ] )
169
- ) {
170
- options . headers [ 'content-length' ] = uploadBodySize ;
165
+ if ( is . undefined ( options . headers [ 'content-length' ] ) && is . undefined ( options . headers [ 'transfer-encoding' ] ) ) {
166
+ if ( uploadBodySize > 0 || options . method === 'PUT' ) {
167
+ options . headers [ 'content-length' ] = uploadBodySize ;
168
+ }
171
169
}
172
170
173
171
for ( const hook of options . hooks . beforeRequest ) {
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ test('transform names to lowercase', async t => {
80
80
t . is ( headers [ 'user-agent' ] , 'test' ) ;
81
81
} ) ;
82
82
83
- test ( 'zero content-length' , async t => {
83
+ test ( 'setting content-length to 0 ' , async t => {
84
84
const { body} = await got ( s . url , {
85
85
headers : {
86
86
'content-length' : 0
@@ -91,6 +91,14 @@ test('zero content-length', async t => {
91
91
t . is ( headers [ 'content-length' ] , '0' ) ;
92
92
} ) ;
93
93
94
+ test ( 'sets content-length to 0 when requesting PUT with empty body' , async t => {
95
+ const { body} = await got ( s . url , {
96
+ method : 'PUT'
97
+ } ) ;
98
+ const headers = JSON . parse ( body ) ;
99
+ t . is ( headers [ 'content-length' ] , '0' ) ;
100
+ } ) ;
101
+
94
102
test ( 'form-data manual content-type' , async t => {
95
103
const form = new FormData ( ) ;
96
104
form . append ( 'a' , 'b' ) ;
You can’t perform that action at this time.
0 commit comments