[英]NodeJS http server response headers are blank when headers are set for a request
[英]How to check implicitly set http headers with golang server?
我創建了一個簡單的http2服務器,如果我用curl向它發送請求,它會響應一些標頭,盡管我沒有明確設置它們。 如何在requesthandling函數(sayhello)中訪問它們? 我的代碼(我以前從未使用過golang)
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)
}
}
發送請求:
curl --head-不安全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
我可以看到一些標頭被發回,我明確設置的標頭也收到了,但是輸出
去運行server.go
[RESPONSE][Header] Myfirst : [golangQuestion]
我如何才能訪問未明確設置但也通過curl接收的其他標頭? 我遍歷了w.Headers ,但是它不包含隱式設置的頭
for k, v := range w.Header() {
fmt.Println("[RESPONSE][Header]", k,":", v)
}
我期望go run server.go的輸出應如下所示:
[RESPONSE][Header] Myfirst : [golangQuestion]
[RESPONSE][Header] Date: [2019.02.12 ]
[RESPONSE][Header] Content-Length: [6]
當您調用ResponseWriter.Write()
時,將自動發送這些標頭。 引用其文檔:
// 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()
僅包含顯式設置的標頭。 Content-Type
和Content-Length
由w.Write()
發送。
注意:如果要禁止顯示此類自動標頭,則必須將其值設置為nil
,例如:
w.Header()["Date"] = nil
另請注意,如果您手動設置此類標頭的值,這些值將被發送而不會更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.