Skip to content

Commit

Permalink
Merge pull request #170 from Matt-Pesce/http_content_length_fix
Browse files Browse the repository at this point in the history
Ensure Content-Length is Sent
  • Loading branch information
b5 committed Jun 11, 2022
2 parents ba8c6e2 + fe9c49a commit 7fb7ff9
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func setBody(req *http.Request, body starlark.String, formData *starlark.Dict, f
return err
}
req.Body = ioutil.NopCloser(strings.NewReader(uq))
// Specifying the Content-Length ensures that https://go.dev/src/net/http/transfer.go doesnt specify Transfer-Encoding: chunked which is not supported by some endpoints.
// This is required when using ioutil.NopCloser method for the request body (see ShouldSendChunkedRequestBody() in the library mentioned above).
req.ContentLength = int64(len(uq))

return nil
}

Expand All @@ -249,6 +253,7 @@ func setBody(req *http.Request, body starlark.String, formData *starlark.Dict, f
return err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(data))
req.ContentLength = int64(len(data))
}

if formData != nil && formData.Len() > 0 {
Expand Down Expand Up @@ -279,6 +284,7 @@ func setBody(req *http.Request, body starlark.String, formData *starlark.Dict, f
case formEncodingURL, "":
contentType = formEncodingURL
req.Body = ioutil.NopCloser(strings.NewReader(form.Encode()))
req.ContentLength = int64(len(form.Encode()))

case formEncodingMultipart:
var b bytes.Buffer
Expand Down

0 comments on commit 7fb7ff9

Please sign in to comment.