Skip to content

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

@GordonJiang

Description

@GordonJiang

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions