Skip to content

Commit 3e81215

Browse files
committed
chore: Refactor logRequestBody to improve request body preview logging
1 parent 994eafa commit 3e81215

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

httpclient/multipartrequest.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"mime/multipart"
1010
"net/http"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
@@ -193,19 +194,30 @@ func trackUploadProgress(file *os.File, writer io.Writer, totalSize int64, log l
193194
// logRequestBody logs the constructed request body for debugging purposes.
194195
func logRequestBody(body *bytes.Buffer, log logger.Logger) {
195196
bodyBytes := body.Bytes()
196-
bodyLen := len(bodyBytes)
197+
bodyStr := string(bodyBytes)
198+
firstBoundaryIndex := strings.Index(bodyStr, "---")
199+
lastBoundaryIndex := strings.LastIndex(bodyStr, "---")
200+
boundaryLength := 3 // Length of "---"
201+
trailingCharCount := 20
202+
203+
if firstBoundaryIndex != -1 && lastBoundaryIndex != -1 && firstBoundaryIndex != lastBoundaryIndex {
204+
// Content before the first boundary
205+
preBoundary := bodyStr[:firstBoundaryIndex+boundaryLength]
206+
// 20 characters after the first boundary
207+
afterFirstBoundary := bodyStr[firstBoundaryIndex+boundaryLength : firstBoundaryIndex+boundaryLength+trailingCharCount]
208+
// 20 characters before the last boundary
209+
beforeLastBoundary := bodyStr[lastBoundaryIndex-trailingCharCount : lastBoundaryIndex]
210+
// Everything after the last boundary
211+
postBoundary := bodyStr[lastBoundaryIndex:]
197212

198-
if bodyLen > 20 {
199-
firstPart := string(bodyBytes[:10])
200-
lastPart := string(bodyBytes[bodyLen-10:])
201-
fullBodyPreview := string(bodyBytes[10 : bodyLen-10])
202213
log.Info("Request body preview",
203-
zap.String("first_10_characters", firstPart),
204-
zap.String("trailing_10_characters", lastPart),
205-
zap.String("full_body_preview", fullBodyPreview))
214+
zap.String("pre_boundary", preBoundary),
215+
zap.String("after_first_boundary", afterFirstBoundary),
216+
zap.String("before_last_boundary", beforeLastBoundary),
217+
zap.String("post_boundary", postBoundary))
206218
} else {
207219
log.Info("Request body preview",
208-
zap.String("body", string(bodyBytes)))
220+
zap.String("body", bodyStr))
209221
}
210222
}
211223

0 commit comments

Comments
 (0)