AWS SDK for Go (PILOT)
API Reference

PREVIEW DOCUMENTATION - This is a preview of a new format for the AWS SDK for Go API Reference documentation. For the current AWS SDK for Go API Reference, see https://docs.aws.amazon.com/sdk-for-go/api/.

We welcome your feedback on this new version of the documentation. Send your comments to aws-sdkdocs-feedback@amazon.com.

BuildContentLengthHandler

import "github.com/aws/aws-sdk-go/aws/corehandlers"

var BuildContentLengthHandler = request.NamedHandler{Name: "core.BuildContentLengthHandler", Fn: func(r *request.Request) { var length int64 if slength := r.HTTPRequest.Header.Get("Content-Length"); slength != "" { length, _ = strconv.ParseInt(slength, 10, 64) } else { if r.Body != nil { var err error length, err = aws.SeekerLen(r.Body) if err != nil { r.Error = awserr.New(request.ErrCodeSerialization, "failed to get request body's length", err) return } } } if length > 0 { r.HTTPRequest.ContentLength = length r.HTTPRequest.Header.Set("Content-Length", fmt.Sprintf("%d", length)) } else { r.HTTPRequest.ContentLength = 0 r.HTTPRequest.Header.Del("Content-Length") } }}

BuildContentLengthHandler builds the content length of a request based on the body, or will use the HTTPRequest.Header's "Content-Length" if defined. If unable to determine request body length and no "Content-Length" was specified it will panic.

The Content-Length will only be added to the request if the length of the body is greater than 0. If the body is empty or the current `Content-Length` header is <= 0, the header will also be stripped.

BuildContentLengthHandler

On this page: