diff --git a/api/server/middleware/middleware.go b/api/server/middleware/middleware.go index d935fa265ffe..b85e59e895b6 100644 --- a/api/server/middleware/middleware.go +++ b/api/server/middleware/middleware.go @@ -50,7 +50,7 @@ func ContentTypeHandler(acceptedMediaTypes []string) mux.MiddlewareFunc { accepted := false for _, acceptedType := range acceptedMediaTypes { - if strings.TrimSpace(contentType) == strings.TrimSpace(acceptedType) { + if strings.Contains(strings.TrimSpace(contentType), strings.TrimSpace(acceptedType)) { accepted = true break } diff --git a/api/server/middleware/middleware_test.go b/api/server/middleware/middleware_test.go index 5869de26b003..3b899d74a59e 100644 --- a/api/server/middleware/middleware_test.go +++ b/api/server/middleware/middleware_test.go @@ -96,6 +96,11 @@ func TestContentTypeHandler(t *testing.T) { expectedStatusCode: http.StatusOK, isGet: true, }, + { + name: "Content type contains charset is ok", + contentType: "application/json; charset=utf-8", + expectedStatusCode: http.StatusOK, + }, } for _, tt := range tests { diff --git a/validator/client/beacon-api/json_rest_handler.go b/validator/client/beacon-api/json_rest_handler.go index 890a10e26be6..0877bff1019b 100644 --- a/validator/client/beacon-api/json_rest_handler.go +++ b/validator/client/beacon-api/json_rest_handler.go @@ -109,7 +109,7 @@ func decodeResp(httpResp *http.Response, resp interface{}) error { return errors.Wrapf(err, "failed to read response body for %s", httpResp.Request.URL) } - if httpResp.Header.Get("Content-Type") != api.JsonMediaType { + if !strings.Contains(httpResp.Header.Get("Content-Type"), api.JsonMediaType) { // 2XX codes are a success if strings.HasPrefix(httpResp.Status, "2") { return nil diff --git a/validator/client/beacon-api/json_rest_handler_test.go b/validator/client/beacon-api/json_rest_handler_test.go index d28729eee652..264bff59a60a 100644 --- a/validator/client/beacon-api/json_rest_handler_test.go +++ b/validator/client/beacon-api/json_rest_handler_test.go @@ -99,7 +99,16 @@ func Test_decodeResp(t *testing.T) { type j struct { Foo string `json:"foo"` } - + t.Run("200 JSON with charset", func(t *testing.T) { + body := bytes.Buffer{} + r := &http.Response{ + Status: "200", + StatusCode: http.StatusOK, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {"application/json; charset=utf-8"}}, + } + require.NoError(t, decodeResp(r, nil)) + }) t.Run("200 non-JSON", func(t *testing.T) { body := bytes.Buffer{} r := &http.Response{