Skip to content

Commit df4e9fa

Browse files
authored
fix(client): validate content-type starting with json (#1681)
1 parent a5694eb commit df4e9fa

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

scw/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,12 @@ func (c *Client) do(req *ScalewayRequest, res interface{}) (sdkErr error) {
235235
if res != nil {
236236
contentType := httpResponse.Header.Get("Content-Type")
237237

238-
switch contentType {
239-
case "application/json":
238+
if strings.HasPrefix(contentType, "application/json") {
240239
err = json.NewDecoder(httpResponse.Body).Decode(&res)
241240
if err != nil {
242241
return errors.Wrap(err, "could not parse %s response body", contentType)
243242
}
244-
default:
243+
} else {
245244
buffer, isBuffer := res.(io.Writer)
246245
if !isBuffer {
247246
return errors.Wrap(err, "could not handle %s response body with %T result type", contentType, buffer)

scw/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func hasResponseError(res *http.Response) error {
102102
newErr.RawBody = body
103103

104104
// The error content is not encoded in JSON, only returns HTTP data.
105-
if res.Header.Get("Content-Type") != "application/json" {
105+
contentType := res.Header.Get("Content-Type")
106+
if !strings.HasPrefix(contentType, "application/json") {
106107
newErr.Message = res.Status
107108
return newErr
108109
}

0 commit comments

Comments
 (0)