Skip to content

Commit

Permalink
graphql: always set content-type to application/json (#28417)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa authored Oct 27, 2023
1 parent 4cbca51 commit 2d7dba0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions graphql/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
}
Expand Down

0 comments on commit 2d7dba0

Please sign in to comment.