Skip to content

Commit 1cf45ee

Browse files
authored
Merge pull request #194 from hashicorp/bugfix/post-empty-body-content-length
Bugfix: fix mishandling of POST/PUT requests with empty body, causing net/http to send request without a `Content-Length` header
2 parents d11b06c + 7bf089a commit 1cf45ee

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,17 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro
260260
if err != nil {
261261
return nil, 0, err
262262
}
263-
bodyReader = func() (io.Reader, error) {
264-
return bytes.NewReader(buf), nil
263+
if len(buf) == 0 {
264+
bodyReader = func() (io.Reader, error) {
265+
return http.NoBody, nil
266+
}
267+
contentLength = 0
268+
} else {
269+
bodyReader = func() (io.Reader, error) {
270+
return bytes.NewReader(buf), nil
271+
}
272+
contentLength = int64(len(buf))
265273
}
266-
contentLength = int64(len(buf))
267274

268275
// No body provided, nothing to do
269276
case nil:

0 commit comments

Comments
 (0)