Skip to content

Commit 0ee0a50

Browse files
authored
Update multipartrequest.go
1 parent 4daa606 commit 0ee0a50

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

httpclient/multipartrequest.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,68 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger)
422422
}
423423
}
424424
}
425+
426+
// DoImageMultiPartUpload performs a multipart request with a specifically formatted payload.
427+
// This is designed for APIs that expect a very specific multipart format, where the payload
428+
// needs to be constructed manually rather than using the standard multipart writer.
429+
func (c *Client) DoImageMultiPartUpload(method, endpoint string, fileName string, base64Data string, customBoundary string, out interface{}) (*http.Response, error) {
430+
if method != http.MethodPost && method != http.MethodPut {
431+
c.Sugar.Error("HTTP method not supported for multipart request", zap.String("method", method))
432+
return nil, fmt.Errorf("unsupported HTTP method: %s", method)
433+
}
434+
435+
// Format the multipart payload with the specified boundary
436+
payload := fmt.Sprintf("%s\r\n"+
437+
"Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n"+
438+
"Content-Type: image/png\r\n\r\n"+
439+
"data:image/png;name=%s;base64,%s\r\n"+
440+
"%s--",
441+
customBoundary,
442+
fileName,
443+
fileName,
444+
base64Data,
445+
customBoundary)
446+
447+
url := (*c.Integration).GetFQDN() + endpoint
448+
449+
// Create the request with the formatted payload
450+
req, err := http.NewRequest(method, url, strings.NewReader(payload))
451+
if err != nil {
452+
c.Sugar.Errorw("Failed to create request", zap.Error(err))
453+
return nil, fmt.Errorf("failed to create request: %v", err)
454+
}
455+
456+
req.Header.Add("Accept", "application/json")
457+
req.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", strings.TrimPrefix(customBoundary, "---")))
458+
459+
(*c.Integration).PrepRequestParamsAndAuth(req)
460+
461+
c.Sugar.Infow("Sending custom multipart request",
462+
zap.String("method", method),
463+
zap.String("url", url),
464+
zap.String("filename", fileName))
465+
466+
startTime := time.Now()
467+
resp, err := c.http.Do(req)
468+
duration := time.Since(startTime)
469+
470+
if err != nil {
471+
c.Sugar.Errorw("Failed to send request",
472+
zap.String("method", method),
473+
zap.String("endpoint", endpoint),
474+
zap.Error(err))
475+
return nil, fmt.Errorf("failed to send request: %v", err)
476+
}
477+
478+
c.Sugar.Debugw("Request sent successfully",
479+
zap.String("method", method),
480+
zap.String("endpoint", endpoint),
481+
zap.Int("status_code", resp.StatusCode),
482+
zap.Duration("duration", duration))
483+
484+
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
485+
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
486+
}
487+
488+
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
489+
}

0 commit comments

Comments
 (0)