diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 0ba5f8769..3602cad85 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -2861,16 +2861,16 @@ func (s *Server) writeHTTPServerResponse(r *params.Request, w http.ResponseWrite resp.RunForErrors(func(jsonErr *neorpc.Error) { s.logRequestError(r, jsonErr) }) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + if s.config.EnableCORSWorkaround { + setCORSOriginHeaders(w.Header()) + } if r.In != nil { resp := resp.(abstract) if resp.Error != nil { w.WriteHeader(getHTTPCodeForError(resp.Error)) } } - w.Header().Set("Content-Type", "application/json; charset=utf-8") - if s.config.EnableCORSWorkaround { - setCORSOriginHeaders(w.Header()) - } encoder := json.NewEncoder(w) err := encoder.Encode(resp) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 98d8324ef..352a454c0 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -3350,3 +3350,21 @@ func TestFailedPreconditionShutdown(t *testing.T) { require.Eventually(t, stopped.Load, 5*time.Second, 100*time.Millisecond, "Shutdown should return") } + +func TestErrorResponseContentType(t *testing.T) { + chain, rpcSrv, httpSrv := initClearServerWithServices(t, true, false, false) + defer chain.Close() + defer rpcSrv.Shutdown() + + const ( + expectedContentType = "application/json; charset=utf-8" + req = `{"jsonrpc":"2.0", "method":"unknown","params":[]}` + ) + + cl := http.Client{Timeout: time.Second} + resp, err := cl.Post(httpSrv.URL, "application/json", strings.NewReader(req)) + require.NoErrorf(t, err, "could not make a POST request") + resp.Body.Close() + contentType := resp.Header.Get("Content-Type") + require.Equal(t, expectedContentType, contentType) +}