You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
Working as intended.
WriteHeader is supposed to write (all) the headers, so when it's called, it
will write the current Content-Type. Later update to the Header map won't
affect header that has already been written.
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.
The text was updated successfully, but these errors were encountered: