@@ -428,10 +428,16 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger)
428
428
// This is designed for APIs that expect a very specific multipart format, where the payload
429
429
// needs to be constructed manually rather than using the standard multipart writer.
430
430
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 ))
435
441
436
442
// Format the multipart payload with the specified boundary
437
443
payload := fmt .Sprintf ("%s\r \n " +
@@ -445,24 +451,55 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
445
451
base64Data ,
446
452
customBoundary )
447
453
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
+
448
470
url := (* c .Integration ).GetFQDN () + endpoint
471
+ c .Sugar .Debugw ("Constructed URL" , zap .String ("fullURL" , url ))
449
472
450
473
// Create the request with the formatted payload
451
474
req , err := http .NewRequest (method , url , strings .NewReader (payload ))
452
475
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 ))
454
480
return nil , fmt .Errorf ("failed to create request: %v" , err )
455
481
}
456
482
483
+ // Set headers with clean boundary
484
+ contentTypeHeader := fmt .Sprintf ("multipart/form-data; boundary=---%s" , cleanBoundary )
457
485
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 ))
459
491
460
492
(* c .Integration ).PrepRequestParamsAndAuth (req )
461
493
494
+ c .Sugar .Debugw ("Request headers after auth" ,
495
+ zap .Any ("headers" , req .Header ))
496
+
462
497
c .Sugar .Infow ("Sending custom multipart request" ,
463
498
zap .String ("method" , method ),
464
499
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" )))
466
503
467
504
startTime := time .Now ()
468
505
resp , err := c .http .Do (req )
@@ -472,7 +509,8 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
472
509
c .Sugar .Errorw ("Failed to send request" ,
473
510
zap .String ("method" , method ),
474
511
zap .String ("endpoint" , endpoint ),
475
- zap .Error (err ))
512
+ zap .Error (err ),
513
+ zap .Duration ("requestDuration" , duration ))
476
514
return nil , fmt .Errorf ("failed to send request: %v" , err )
477
515
}
478
516
@@ -482,7 +520,12 @@ func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string
482
520
zap .Int ("status_code" , resp .StatusCode ),
483
521
zap .Duration ("duration" , duration ))
484
522
523
+ // Log response headers
524
+ c .Sugar .Debugw ("Response headers" ,
525
+ zap .Any ("headers" , resp .Header ))
526
+
485
527
if resp .StatusCode >= 200 && resp .StatusCode < 300 {
528
+ c .Sugar .Info ("Request succeeded, processing response" )
486
529
return resp , response .HandleAPISuccessResponse (resp , out , c .Sugar )
487
530
}
488
531
0 commit comments