Skip to content

net/http header not being set as expected #50587

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

Closed
evanlurvey opened this issue Jan 12, 2022 · 1 comment
Closed

net/http header not being set as expected #50587

evanlurvey opened this issue Jan 12, 2022 · 1 comment

Comments

@evanlurvey
Copy link

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

tested with go 1.17.5 and 1.17.6

Does this issue reproduce with the latest release?

yes

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

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/elurvey/Library/Caches/go-build"
GOENV="/Users/elurvey/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/elurvey/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/elurvey/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/pq/pmt4ss412gz7_4r9dl_ny35c0000gn/T/go-build3784969989=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import "net/http"

func transactionsEndpoint(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
	w.Header().Set("Content-Type", "application/json")
	w.Write([]byte(`{"why": "you no work"}`))
}

func main() {
	handler := http.HandlerFunc(transactionsEndpoint)
	http.Handle("/", handler)
	http.ListenAndServe(":8080", nil)
}

What did you expect to see?

I expect to see the header set Content-Type header set to application/json

What did you see instead?

header is set to text/plain instead

$ curl localhost:8080 -v
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 12 Jan 2022 21:51:12 GMT
< Content-Length: 22
< Content-Type: text/plain; charset=utf-8
< 
* Connection #0 to host localhost left intact
{"why": "you no work"}* Closing connection 0
@seankhliao
Copy link
Member

Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Jan 12, 2023
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

3 participants