Skip to content

Commit 8ace545

Browse files
authored
Merge pull request #268 from deploymenttheory/add-icon-upload-handling
feat: enhance DoImageMultiPartUpload with detailed logging and bounda…
2 parents 34523b5 + beb195f commit 8ace545

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

httpclient/multipartrequest.go

+51-8
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,16 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger)
428428
// This is designed for APIs that expect a very specific multipart format, where the payload
429429
// needs to be constructed manually rather than using the standard multipart writer.
430430
func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string, base64Data string, customBoundary string, out interface{}) (*http.Response, error) {
431-
if method != http.MethodPost && method != http.MethodPut {
432-
c.Sugar.Error("HTTP method not supported for multipart request", zap.String("method", method))
433-
return nil, fmt.Errorf("unsupported HTTP method: %s", method)
434-
}
431+
c.Sugar.Infow("Starting DoImageMultiPartUpload",
432+
zap.String("method", method),
433+
zap.String("endpoint", endpoint),
434+
zap.String("fileName", fileName),
435+
zap.String("originalBoundary", customBoundary))
436+
437+
// Remove any leading hyphens from the boundary when setting in header
438+
cleanBoundary := strings.TrimPrefix(customBoundary, "-----")
439+
c.Sugar.Debugw("Processed boundary",
440+
zap.String("cleanBoundary", cleanBoundary))
435441

436442
// Format the multipart payload with the specified boundary
437443
payload := fmt.Sprintf("%s\r\n"+
@@ -445,24 +451,55 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
445451
base64Data,
446452
customBoundary)
447453

454+
// Log the payload structure (not the full base64 data)
455+
payloadPreview := fmt.Sprintf("%s\r\n"+
456+
"Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n"+
457+
"Content-Type: image/png\r\n\r\n"+
458+
"data:image/png;name=%s;base64,[BASE64_DATA_LENGTH: %d]\r\n"+
459+
"%s--",
460+
customBoundary,
461+
fileName,
462+
fileName,
463+
len(base64Data),
464+
customBoundary)
465+
466+
c.Sugar.Debugw("Constructed payload",
467+
zap.String("payloadStructure", payloadPreview),
468+
zap.Int("totalPayloadLength", len(payload)))
469+
448470
url := (*c.Integration).GetFQDN() + endpoint
471+
c.Sugar.Debugw("Constructed URL", zap.String("fullURL", url))
449472

450473
// Create the request with the formatted payload
451474
req, err := http.NewRequest(method, url, strings.NewReader(payload))
452475
if err != nil {
453-
c.Sugar.Errorw("Failed to create request", zap.Error(err))
476+
c.Sugar.Errorw("Failed to create request",
477+
zap.Error(err),
478+
zap.String("method", method),
479+
zap.String("url", url))
454480
return nil, fmt.Errorf("failed to create request: %v", err)
455481
}
456482

483+
// Set headers with clean boundary
484+
contentTypeHeader := fmt.Sprintf("multipart/form-data; boundary=---%s", cleanBoundary)
457485
req.Header.Add("Accept", "application/json")
458-
req.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", strings.TrimPrefix(customBoundary, "---")))
486+
req.Header.Add("Content-Type", contentTypeHeader)
487+
488+
c.Sugar.Debugw("Request headers before auth",
489+
zap.Any("headers", req.Header),
490+
zap.String("contentType", contentTypeHeader))
459491

460492
(*c.Integration).PrepRequestParamsAndAuth(req)
461493

494+
c.Sugar.Debugw("Request headers after auth",
495+
zap.Any("headers", req.Header))
496+
462497
c.Sugar.Infow("Sending custom multipart request",
463498
zap.String("method", method),
464499
zap.String("url", url),
465-
zap.String("filename", fileName))
500+
zap.String("filename", fileName),
501+
zap.String("contentType", req.Header.Get("Content-Type")),
502+
zap.String("accept", req.Header.Get("Accept")))
466503

467504
startTime := time.Now()
468505
resp, err := c.http.Do(req)
@@ -472,7 +509,8 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
472509
c.Sugar.Errorw("Failed to send request",
473510
zap.String("method", method),
474511
zap.String("endpoint", endpoint),
475-
zap.Error(err))
512+
zap.Error(err),
513+
zap.Duration("requestDuration", duration))
476514
return nil, fmt.Errorf("failed to send request: %v", err)
477515
}
478516

@@ -482,7 +520,12 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
482520
zap.Int("status_code", resp.StatusCode),
483521
zap.Duration("duration", duration))
484522

523+
// Log response headers
524+
c.Sugar.Debugw("Response headers",
525+
zap.Any("headers", resp.Header))
526+
485527
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
528+
c.Sugar.Info("Request succeeded, processing response")
486529
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
487530
}
488531

0 commit comments

Comments
 (0)