Skip to content

Commit f55f77f

Browse files
authored
Update success.go
1 parent 4c2aa01 commit f55f77f

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

response/success.go

+28-9
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,46 @@ func logResponseDetails(resp *http.Response, bodyBytes []byte, log logger.Logger
7171
log.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header))
7272
}
7373

74-
// handleBinaryData checks if the response should be treated as binary data based on the Content-Type or Content-Disposition headers. It assigns the response body to 'out' if 'out' is of type *[]byte.
74+
75+
// handleBinaryData checks if the response should be treated as binary data based on the Content-Type or Content-Disposition headers.
76+
// It supports writing the response body to an output parameter of type *[]byte or io.Writer.
7577
func handleBinaryData(contentType, contentDisposition string, bodyBytes []byte, log logger.Logger, out interface{}) error {
7678
// Check if response is binary data either by Content-Type or Content-Disposition
7779
if strings.Contains(contentType, "application/octet-stream") || strings.HasPrefix(contentDisposition, "attachment") {
78-
// Assert that 'out' is of the correct type to receive binary data
79-
if outPointer, ok := out.(*[]byte); ok {
80-
*outPointer = bodyBytes // Assign the response body to 'out'
81-
log.Debug("Handled binary data", // Log handling of binary data
80+
switch out := out.(type) {
81+
case *[]byte:
82+
*out = bodyBytes // Assign the response body to 'out'
83+
log.Debug("Handled binary data as []byte",
8284
zap.String("Content-Type", contentType),
8385
zap.String("Content-Disposition", contentDisposition),
8486
)
85-
return nil
86-
} else {
87-
errMsg := "output parameter is not a *[]byte for binary data"
88-
log.Error("Binary data handling error", // Log error for incorrect 'out' type
87+
88+
case io.Writer:
89+
_, err := out.Write(bodyBytes) // Write the response body to 'out'
90+
if err != nil {
91+
errMsg := "failed to write binary data to io.Writer"
92+
log.Error("Binary data writing error",
93+
zap.String("error", errMsg),
94+
zap.String("Content-Type", contentType),
95+
zap.String("Content-Disposition", contentDisposition),
96+
)
97+
return fmt.Errorf(errMsg)
98+
}
99+
log.Debug("Handled binary data as io.Writer",
100+
zap.String("Content-Type", contentType),
101+
zap.String("Content-Disposition", contentDisposition),
102+
)
103+
104+
default:
105+
errMsg := "output parameter is not a *[]byte or io.Writer for binary data"
106+
log.Error("Binary data handling error",
89107
zap.String("error", errMsg),
90108
zap.String("Content-Type", contentType),
91109
zap.String("Content-Disposition", contentDisposition),
92110
)
93111
return fmt.Errorf(errMsg)
94112
}
113+
return nil
95114
}
96115
return nil // If not binary data, no action needed
97116
}

0 commit comments

Comments
 (0)