Skip to content

Commit 9a966ec

Browse files
szmarczaksindresorhus
authored andcommittedAug 24, 2018
Set content-length header to zero when doing a PUT request with no body (#584)
Fixes #582
·
v14.4.7v9.2.0
1 parent 716b914 commit 9a966ec

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed
 

‎source/request-as-event-emitter.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,10 @@ module.exports = options => {
162162
try {
163163
uploadBodySize = await getBodySize(options);
164164

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+
}
171169
}
172170

173171
for (const hook of options.hooks.beforeRequest) {

‎test/headers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test('transform names to lowercase', async t => {
8080
t.is(headers['user-agent'], 'test');
8181
});
8282

83-
test('zero content-length', async t => {
83+
test('setting content-length to 0', async t => {
8484
const {body} = await got(s.url, {
8585
headers: {
8686
'content-length': 0
@@ -91,6 +91,14 @@ test('zero content-length', async t => {
9191
t.is(headers['content-length'], '0');
9292
});
9393

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+
94102
test('form-data manual content-type', async t => {
95103
const form = new FormData();
96104
form.append('a', 'b');

0 commit comments

Comments
 (0)
Please sign in to comment.