Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: ResponseWriter doesn't overwrite content-type after WriteHeader is called. #17083

Closed
GordonJiang opened this issue Sep 13, 2016 · 2 comments

Comments

@GordonJiang
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.7

What operating system and processor architecture are you using (go env)?

ubuntu, amd64

What did you do?

in a http server handler,
first call w.WriteHeader(http.StatusOK)
then call w.Header().Set("Content-Type", "application/json")
then w.Write(somedata compressed by gzip)

What did you expect to see?

The Content-Type in response message captured by troubleshooting tool of chrome/firefox becomes application/x-gzip

What did you see instead?

The Content-Type should be application/json as I set.

On the other hand, if I change the order. call w.Header().Set("Content-Type", "application/json") ahead of the w.WriteHeader(http.StatusOK), the Content-Type keeps what it is. Seems setting status ahead of write content-type trigger the logic to DetectContentType automatically. It's overwrite whatever content-type I set later.
Because of that, gzip content can't be parsed in firefox. Of course, I worked around it by changing the code to set content-type first. But it apparently a trap since there is no doc saying you should always set content-type before set status by WriteHeader.

@odeke-em odeke-em changed the title golang http server overwrite content-type I set if WriteHeader(http.StatusOK) is called first. net/http: ResponseWriter doesn't overwrite content-type after WriteHeader is called. Sep 13, 2016
@odeke-em
Copy link
Member

@GordonJiang this behavior is documented in https://golang.org/pkg/net/http/#ResponseWriter
screen shot 2016-09-12 at 6 11 46 pm

as the documentation for property Header().

Closing this issue unless someone else thinks we should do more.
/cc @bradfitz.

@minux
Copy link
Member

minux commented Sep 13, 2016 via email

@golang golang locked and limited conversation to collaborators Sep 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants