From 2d7dba024d76603398907a595da98ad4df81b858 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 27 Oct 2023 22:50:59 +0800 Subject: [PATCH] graphql: always set content-type to application/json (#28417) --------- Signed-off-by: jsvisa --- graphql/graphql_test.go | 3 +++ graphql/service.go | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 540a56778..a83d6bbd4 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -169,6 +169,9 @@ func TestGraphQLBlockSerialization(t *testing.T) { if tt.code != resp.StatusCode { t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code) } + if ctype := resp.Header.Get("Content-Type"); ctype != "application/json" { + t.Errorf("testcase %d \nwrong Content-Type, have: %v, want: %v", i, ctype, "application/json") + } } } diff --git a/graphql/service.go b/graphql/service.go index f33e76305..584165bdb 100644 --- a/graphql/service.go +++ b/graphql/service.go @@ -73,12 +73,12 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // Setting this disables gzip compression in package node. - w.Header().Set("transfer-encoding", "identity") + w.Header().Set("Transfer-Encoding", "identity") // Flush the response. Since we are writing close to the response timeout, // chunked transfer encoding must be disabled by setting content-length. - w.Header().Set("content-type", "application/json") - w.Header().Set("content-length", strconv.Itoa(len(responseJSON))) + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Length", strconv.Itoa(len(responseJSON))) w.Write(responseJSON) if flush, ok := w.(http.Flusher); ok { flush.Flush() @@ -97,10 +97,10 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "application/json") if len(response.Errors) > 0 { w.WriteHeader(http.StatusBadRequest) } - w.Header().Set("Content-Type", "application/json") w.Write(responseJSON) }) }