Skip to content

Commit

Permalink
Merge pull request #194 from hashicorp/bugfix/post-empty-body-content…
Browse files Browse the repository at this point in the history
…-length

Bugfix: fix mishandling of POST/PUT requests with empty body, causing net/http to send request without a `Content-Length` header
  • Loading branch information
manicminer committed Jun 6, 2023
2 parents d11b06c + 7bf089a commit 1cf45ee
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions client.go
Expand Up @@ -260,10 +260,17 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro
if err != nil {
return nil, 0, err
}
bodyReader = func() (io.Reader, error) {
return bytes.NewReader(buf), nil
if len(buf) == 0 {
bodyReader = func() (io.Reader, error) {
return http.NoBody, nil
}
contentLength = 0
} else {
bodyReader = func() (io.Reader, error) {
return bytes.NewReader(buf), nil
}
contentLength = int64(len(buf))
}
contentLength = int64(len(buf))

// No body provided, nothing to do
case nil:
Expand Down

0 comments on commit 1cf45ee

Please sign in to comment.