1

I created a simple http2 server, If I send a request to it with curl, it responds with some headers, although I did not set them explicity. How can I acces them inside the requesthandling function ( sayhello )? My code ( I've never used golang before)

server.go

package main

import (
  "net/http"
  "strings"
  "fmt"
  "github.com/gorilla/mux"
  "golang.org/x/net/http2"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
  message := r.URL.Path
  message = strings.TrimPrefix(message, "/")
  message = "Hello " + message

  w.Header().Set("myFirst", "golangQuestion")
  w.Write([]byte(message))
  for k, v := range w.Header() {
    fmt.Println("[RESPONSE][Header]", k,":", v)
    }
}

func main() {
    router := mux.NewRouter()
    router.PathPrefix("/").HandlerFunc(sayHello) // catch everything else rule
    var srv = &http.Server{
        Addr: "127.0.0.1:8081",
    }
    http2.ConfigureServer(srv, nil)
    srv.Handler = router
    sslCert := "./ssl.cert"
    sslKey := "./ssl.key"
    if err := srv.ListenAndServeTLS(sslCert, sslKey); err != nil {
        panic(err)
    }
}

Sending request:

curl --head --insecure https://127.0.0.1:8081

HTTP/1.1 200 OK
Myfirst: golangQuestion
Date: Tue, 18 Jun 2019 09:18:29 GMT
Content-Length: 6
Content-Type: text/plain; charset=utf-8

I can see that some headers are sent back, the one which I set explicitly is also recieved, but the output of

go run server.go

[RESPONSE][Header] Myfirst : [golangQuestion]

How can I acces the other headers, which were not explicitly set, but recieved by curl as well? I loopd through w.Headers, but it did not contain the implicitly set headers

  for k, v := range w.Header() {
    fmt.Println("[RESPONSE][Header]", k,":", v)
    }

My expectation that the output of go run server.go shall be something like this:

[RESPONSE][Header] Myfirst : [golangQuestion]
[RESPONSE][Header] Date: [2019.02.12 ]
[RESPONSE][Header] Content-Length: [6]

1 Answer 1

3

Those headers are sent automatically when you call ResponseWriter.Write(). Quoting from its doc:

// Write writes the data to the connection as part of an HTTP reply.
//
// If WriteHeader has not yet been called, Write calls
// WriteHeader(http.StatusOK) before writing the data. If the Header
// does not contain a Content-Type line, Write adds a Content-Type set
// to the result of passing the initial 512 bytes of written data to
// DetectContentType. Additionally, if the total size of all written
// data is under a few KB and there are no Flush calls, the
// Content-Length header is added automatically.
//
// Depending on the HTTP protocol version and the client, calling
// Write or WriteHeader may prevent future reads on the
// Request.Body. For HTTP/1.x requests, handlers should read any
// needed request body data before writing the response. Once the
// headers have been flushed (due to either an explicit Flusher.Flush
// call or writing enough data to trigger a flush), the request body
// may be unavailable. For HTTP/2 requests, the Go HTTP server permits
// handlers to continue to read the request body while concurrently
// writing the response. However, such behavior may not be supported
// by all HTTP/2 clients. Handlers should read before writing if
// possible to maximize compatibility.
Write([]byte) (int, error)

ResponseWriter.Header() contains only the headers set explicitly. The Content-Type and Content-Length were sent by w.Write().

Note: if you want to suppress such automatic headers, you have to set their values to nil, e.g.:

w.Header()["Date"] = nil

Also note that if you set the values of such headers manually, those values will be sent without being changed.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok I saw that too, I figured the headers are added automatically, becouse I did not set them, but they did appear. My looping through the w.Header object is after calling ResponseWriter.Write(). My problem is not that some go library is adding headers, be it. My problem is how can I acces them inside the sayhello function
@user3598726 You can't access them. You may suppress them if you don't want them sent (see edited answer), or you may set them explicitly to some other value, if you set them, they won't be changed / overwritten.
Thanks, How can I know which one to supress If I don't know which one will be set? I can't find documentation about which headers will be added implicitly by the library
@user3598726 ResponseWriter.Write() documents it may add Content-Type and Content-Length. And another automatic header is Date. As your test shows, there is no other at the moment.
Thanks u helped a lot, I still can't wrap my head around not being able to acces those headers. Shall I use a lower level library?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.